Sql server 实体状态在需要修改时被分离
我正在尝试使用Sql server 实体状态在需要修改时被分离,sql-server,linq,entity-framework-core,asp.net-core-3.1,dbcontext,Sql Server,Linq,Entity Framework Core,Asp.net Core 3.1,Dbcontext,我正在尝试使用EFCore 3.1修改数据并保存对数据库的更改。 但是我的修改没有保存到数据库中,因此在进一步调查后,我发现在我从上下文中提取实体后,它的状态是分离的,而不是附加的, 这就是为什么更改没有被保存,因为它们一开始就没有被跟踪 我不明白为什么会发生这种情况,我确保在获取实体时没有添加AsNoTracking() 以下是我的类和方法: public class UserSettingsDataAccess : IUserSettingsDataAccess private r
EFCore 3.1修改数据并保存对数据库的更改。
但是我的修改没有保存到数据库中,因此在进一步调查后,我发现在我从上下文中提取实体后,它的状态是分离的
,而不是附加的
,
这就是为什么更改没有被保存,因为它们一开始就没有被跟踪
我不明白为什么会发生这种情况,我确保在获取实体时没有添加AsNoTracking()
以下是我的类和方法:
public class UserSettingsDataAccess : IUserSettingsDataAccess
private readonly NotificationDBContext _context;
private readonly IReminderDatesDataAccess _reminderDatesDataAccess;
public UserSettingsDataAccess(NotificationDBContext context, IReminderDatesDataAccess reminderDatesDataAccess)
{
_context = context;
_reminderDatesDataAccess = reminderDatesDataAccess;
}
public bool ToggleRemindersForAppointmentAsync(int appointment_id)
{
Appointments appointment = _appointmentDataAccess.GetByIdWithReminders(appointment_id);
if (appointment == null || appointment.Reminders == null)
return bool.Parse(null);
var x = _context.Entry(appointment).State;
appointment.Reminders.IsActive = !appointment.Reminders.IsActive;
var y = _context.Entry(appointment).State;
_context.SaveChanges();
var z = _context.Entry(appointment).State;
return appointment.Reminders.IsActive.Value;
}
//rest of code is omitted for brevity
}
此方法使用另一种方法获取约会、切换其提醒、保存更改并返回新的提醒状态。所有x
、y
、z
变量都具有分离的值。调试时
下面是第二个类,它包含带来约会的方法:
public class AppointmentDataAccess: IAppointmentDataAccess
{
private readonly NotificationDBContext _context;
public ReminderDatesDataAccess(NotificationDBContext context)
{
_context = context;
}
public Appointments GetByIdWithReminders(int appointment_id)
{
return _context.Appointments.Where(a => a.Id == appointment_id && a.DeletedAt == null)
.Include(a=>a.Reminders).FirstOrDefault();
}
}
Startup.cs
:
services.AddDbContext<NotificationDBContext>(options => options
.UseSqlServer(Configuration.GetConnectionString("Database"))
, ServiceLifetime.Transient,ServiceLifetime.Transient);
services.AddDbContext(选项=>options
.UseSqlServer(Configuration.GetConnectionString(“数据库”))
,ServiceLifetime.Transient,ServiceLifetime.Transient);
而iusersetingsdataaccess
,IAppointmentDataAccess
只是接口
有人能指出为什么会发生这种情况吗?如何修复它?好几个小时来我都快疯了。蒂亚 怀疑第一个方法中的\u context
与第二个方法中的\u context
不是同一个对象,因此当在第一个方法中为实体状态询问\u context
时,上下文没有被跟踪。跟踪是由上下文完成的,而不是实体本身。@NeilW-hmm。。是什么原因造成的?我是说怎么会这样?在这个项目中,我已经做过一千次类似的操作,这在以前从未发生过。托管GetByIdWithMembers
方法的\u appointmentDataAccess
类中声明的\u context
变量在哪里?其中,\u上下文
声明了一个变量,该变量由切换提醒器用于应用程序异步
?每个实例在哪里?谢谢你的问题编辑。上下文被注册为“Transient”,因此每个类都获得自己的NotificationDbContext实例。这就是问题所在。如果要使用DataAccess类,那么就让它成为唯一一个包含对上下文引用的类。然后在该DataAccess类上放置一个SaveChanges方法,该方法可以在原始DbContext上调用SaveChanges。并且从体系结构的角度来看,每个DataAccess类应该只管理其自身范围内的数据。您试图从UserSettingsData管理任命数据,这有点臭。最好在上面创建一个服务层,使用这两个dataaccess类并跨这两个类协调活动,而不是一个dataaccess类试图直接与另一个dataaccess类协作。