[英]Cannot insert explicit value for identity column in 'DentalProcedures' when IDENTITY_INSERT is set to OFF. EF code first
I will try to keep this one short.我会尽量保持简短。 Any help is welcome and appreciated!欢迎和赞赏任何帮助!
I have 2 classes that have a many-to-many relationship and their composite key class.我有 2 个具有多对多关系的类及其复合键 class。 When I'm creating a new "appointment" I want to pick "dental procedures" that are in the system.当我创建一个新的“约会”时,我想选择系统中的“牙科程序”。
All works fine until I reach the AppointmentRepository where I try to save the newly created appointment.一切正常,直到我到达 AppointmentRepository 我尝试保存新创建的约会。 The error is as stated.错误如所述。
I tried to add the我试图添加
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
or或者
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
annotations above the DentalProcedureId
property in the DentalProcedure
class, but nothing works. DentalProcedure
class 中的DentalProcedureId
属性上方的注释,但没有任何效果。 Saved the changes and dropped tables, deleted all of the migrations etc.保存更改并删除表,删除所有迁移等。
DentalProcedure class:牙科程序 class:
public class DentalProcedure
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DentalProcedureId { get; set; }
[Required(ErrorMessage = "The name of the procedure must be specified")]
public string ProcedureName { get; set; }
[Required(ErrorMessage = "The price of the procedure must be specified")]
public decimal ProcedurePrice { get; set; }
public bool isEnabled { get; set; }
public List<CustomerProcedure> CustomerProcedures { get; set; }
public List<AppointmentProcedure> AppointmentProcedures { get; set; }
}
Appointment class:预约class:
public class Appointment
{
[Key]
public int AppointmentId { get; set; }
[Required]
public DateTime AppointmentStart { get; set; }
[Required]
public DateTime AppointmentEnd { get; set; }
[Required]
public string Title { get; set; }
public string ProcedureDescription { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; }
public int WorkDaysId { get; set; }
public WorkDays WorkDays { get; set; }
public List<AppointmentProcedure> AppointmentProcedures { get; set; }
}
AppointmentProcedure class:预约程序class:
public class AppointmentProcedure
{
public int AppointmentId { get; set; }
public Appointment Appointment { get; set; }
public int DentalProcedureId { get; set; }
public DentalProcedure DentalProcedure { get; set; }
public bool ProcedureAppointmentCanceled { get; set; }
}
Home controller: controller:
Appointment appointment = new Appointment
{
AppointmentStart = model.AppointmentStart,
AppointmentEnd = model.AppointmentEnd,
Title = model.Title,
ProcedureDescription = model.ProcedureDescription,
CustomerId = Id,
WorkDaysId = workkWeek.WorkDaysId,
};
foreach (var proc in model.DentalProcedures)
{
if (proc.isEnabled)
{
appointment.AppointmentProcedures = new List<AppointmentProcedure>
{
new AppointmentProcedure
{
Appointment = appointment,
DentalProcedure = proc,
ProcedureAppointmentCanceled = false
}
};
}
}
_appointment.CreateAppointment(appointment);
And the error:和错误:
Thanks once more in advance.再次提前感谢。
PS: I'm still learning so if I forgot to mention something, I apologize in advance! PS:我还在学习中,所以如果我忘了说什么,我提前道歉!
Entity Framework needs to track the object in the database so you need to either attach or (what I usually prefer to do) load it from the database.实体框架需要跟踪数据库中的 object,因此您需要附加或(我通常喜欢这样做)从数据库中加载它。 Here's the changed code:这是更改后的代码:
foreach (var proc in model.DentalProcedures)
{
if (proc.isEnabled)
{
//assuming the DBSet is called Procedures
var dbProc = await db.Procedures.FirstOrDefaultAsync(p => p.DentalProcedureId == id));
appointment.AppointmentProcedures = new List<AppointmentProcedure>
{
new AppointmentProcedure
{
Appointment = appointment,
DentalProcedure = dbProc, //now set the loaded entity
ProcedureAppointmentCanceled = false
}
};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.