简体   繁体   English

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

i am working using EF and I have a table that has an identity property and i tried a lot of solutions but i still get that error. 我正在使用EF,并且我有一个具有身份属性的表,我尝试了很多解决方案,但仍然遇到该错误。 i defined the property in the class as : 我在类中定义该属性为:

[Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int PATIENTDONORID { get; set; } 

and i still get that error and i made the identity option is on in OnModelCreating and i also get the same error 而且我仍然收到该错误,并且我在OnModelCreating中启用了identity选项,并且我也遇到了相同的错误

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           // throw new UnintentionalCodeFirstException();

            modelBuilder.Entity<PATIENT_DONOR>().Property(x => x.PATIENTDONORID)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        }

this is the code that makes insertion and save changes in database: 这是进行插入并将更改保存到数据库中的代码:

foreach (var item in patient.DONORS)
                {
                    if (item.Checked)
                    {
                        db.PATIENT_DONOR.Add(new PATIENT_DONOR() { PATIENT_ID = patient.PATIENT_ID, DONOR_ID = item.ID});
                    }
                }

               // db.Entry(patient).State = EntityState.Modified;
                db.SaveChanges();

What should i do ? 我该怎么办 ?

It is working Correctly now . 现在它可以正常工作。 I realized that After any change in dB I must remove and redrag tables in model. 我意识到,在dB发生任何变化之后,我必须删除并重新拖动模型中的表。 and there is no problems in the code itself 而且代码本身没有问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 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 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 EF Core SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“MenuCategories”中插入标识列的显式值 - EF Core SqlException: Cannot insert explicit value for identity column in table 'MenuCategories' when IDENTITY_INSERT is set to OFF 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表&#39;[table]&#39;中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法为表 [表名] 中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF 出现错误:当IDENTITY_INSERT设置为OFF时,无法在表&#39;Table&#39;中为标识列插入显式值 - Getting Error: Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法为表&#39;ActivityInstances中的标识列插入显式值 - Cannot insert explicit value for identity column in table 'ActivityInstances' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;Recipe&#39;中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Recipe' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法在表&#39;MyTableName&#39;中为标识列插入显式值 - Cannot insert explicit value for identity column in table 'MyTableName' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM