简体   繁体   English

IDENTITY_INSERT设置为OFF时,无法手动为标识插入ID实体插入显式值

[英]Cannot insert explicit value for identity insert ID manually entity when IDENTITY_INSERT is set to OFF

I have a table Worker : 我有一个餐桌Worker

 public class Worker
 {
        public int ID { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string Fathername { get; set; }
        public string email { get; set; }
        public string Company_name { get; set; }    
}  

I try to add a new worker: 我尝试添加新工人:

new Worker { Firstname = "John", Lastname = "Rambo", Fathername = "First_Blood", email = "Rocky@hh.ru", Company_name = "Survive" },

And I get error in application insights from last debug 我从上次调试中获得了应用程序见解中的错误

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时发生错误。 See the inner exception for details. 有关详细信息,请参见内部异常。

System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'Worker' when IDENTITY_INSERT is set to OFF. System.Data.SqlClient.SqlException:当IDENTITY_INSERT设置为OFF时,无法为表'Worker'中的标识列插入显式值。

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔值breakConnection,动作1

Of course I can remove ID and it added automatically. 当然,我可以删除ID并自动添加它。 But I want to add it manually of if it is nulled then generate automatically. 但是我想手动添加它是否为空然后自动生成。

Firstly, your ID column will never be null as you are using the int type and not the nullable int type. 首先,您的ID列永远不会为null,因为您使用的是int类型而不是可为null的int类型。

You can make your int type nullable by changing public int ID { get; set; } 您可以通过更改public int ID { get; set; } public int ID { get; set; } public int ID { get; set; } to public int? ID { get; set; } public int ID { get; set; } public int? ID { get; set; } public int? ID { get; set; }

The reason you are receiving this error is because IDENTITY_INSERT is set to off on your database. 您收到此错误的原因是因为IDENTITY_INSERT在您的数据库上设置为关闭。

If you apply the following attribute/convention to the property in your class and update the changes to the database you should find you are allowed to insert into the identity column. 如果将以下属性/惯例应用于类中的属性,并更新对数据库的更改,则应该允许您将其插入“标识”列。

public class Worker
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ID { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Fathername { get; set; }
    public string email { get; set; }
    public string Company_name { get; set; }    
} 

Alternatively, you can use the OnModelCreating overload as such; 另外,您可以使用OnModelCreating重载。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Worker>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}

暂无
暂无

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

相关问题 实体框架:当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 当 IDENTITY_INSERT 设置为 OFF 时,无法为标识列插入显式值。 (实体框架核心) - Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core) 在 Entity Framework 中将 IDENTITY_INSERT 设置为 OFF 时无法插入显式值 - Cannot insert explicit value when IDENTITY_INSERT is set to OFF in Entity Framework 实体框架抛出无法为表中的“身份”列插入显式值…IDENTITY_INSERT设置为OFF时错误 - Entity Framework Throwing Cannot Insert Explicit Value for Identity Column In Table … When IDENTITY_INSERT is set to OFF Error 使用现有数据库和实体框架的代码优先:当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 实体框架核心:当IDENTITY_INSERT设置为OFF时,无法为表&#39;Relation&#39;中的Identity列插入显式值 - Entity framework core: Cannot insert explicit value for identity column in table 'Relation' when IDENTITY_INSERT is set to OFF Entity Framework Core 6 - 当 IDENTITY_INSERT 设置为 OFF 时,无法在表“角色”中插入标识列的显式值 - Entity Framework Core 6 - Cannot insert explicit value for identity column in table 'Roles' 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时,1:1映射并且无法为表&#39;DivisionParticipant&#39;中的Identity列插入显式值 - 1:1 mapping and Cannot insert explicit value for identity column in table 'DivisionParticipant' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,C#无法为表“出租”中的标识列插入显式值 - C# Cannot insert explicit value for identity column in table “Rentals” when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM