简体   繁体   English

当 IDENTITY_INSERT 设置为 OFF 时,无法在“DentalProcedures”中插入标识列的显式值。 EF代码优先

[英]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.

相关问题 {“当IDENTITY_INSERT设置为OFF时,无法在表&#39;Cantact&#39;中为标识列插入显式值。”} - {“Cannot insert explicit value for identity column in table 'Cantact' when IDENTITY_INSERT is set to OFF.”} “当IDENTITY_INSERT设置为OFF时,无法为表&#39;Movies&#39;中的标识列插入显式值。” - “Cannot insert explicit value for identity column in table 'Movies' when IDENTITY_INSERT is set to OFF.” “当IDENTITY_INSERT设置为OFF时,无法为表{Tags}中的标识列插入显式值。” - “Cannot insert explicit value for identity column in table {Tags} when IDENTITY_INSERT is set to OFF.” 当 IDENTITY_INSERT 设置为 OFF 时,无法为标识列插入显式值。 (实体框架核心) - Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core) 当IDENTITY_INSERT设置为OFF时,获取“无法在表&#39;OrderPromo&#39;中为标识列插入显式值”。 在桌子上没有身份 - Getting 'Cannot insert explicit value for identity column in table 'OrderPromo' when IDENTITY_INSERT is set to OFF.' on table with NO Identity IDENTITY_INSERT设置为OFF时,EF无法为表&#39;X&#39;中的标识列插入显式值 - EF Cannot insert explicit value for identity column in table 'X' when IDENTITY_INSERT is set to OFF IDENTITY_INSERT设置为OFF时,EF无法为表“ PATIENT DONOR”中的标识列插入显式值 - EF Cannot insert explicit value for identity column in table 'PATIENT DONOR' when IDENTITY_INSERT is set to OFF 使用现有数据库和实体框架的代码优先:当IDENTITY_INSERT设置为OFF时,无法为表中的标识列插入显式值 - Code First With Existing Database, Entity Framework: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF EF Core:当 IDENTITY_INSERT 在一对多上设置为 OFF 时,无法在表中插入标识列的显式值 - EF Core: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF on one to many EF Core给我错误当IDENTITY_INSERT设置为OFF时,无法为表“标签”中的标识列插入显式值 - EF Core giving me error Cannot insert explicit value for identity column in table 'Tags' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM