Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当identity_insert设置为OFF时,无法手动插入identity insert ID实体的显式值_C#_Entity - Fatal编程技术网

C# 当identity_insert设置为OFF时,无法手动插入identity insert ID实体的显式值

C# 当identity_insert设置为OFF时,无法手动插入identity insert ID实体的显式值,c#,entity,C#,Entity,我有一个表工作者: 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 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; }    
}  
我尝试添加一个新工作人员:

new Worker { Firstname = "John", Lastname = "Rambo", Fathername = "First_Blood", email = "Rocky@hh.ru", Company_name = "Survive" },
我从上一次调试中得到了应用程序洞察中的错误

Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时出错。有关详细信息,请参见内部异常

System.Data.SqlClient.SqlException:当identity_insert设置为OFF时,无法在表“Worker”中为identity列插入显式值

位于System.Data.SqlClient.SqlConnection.OneError(SqlException异常,布尔断开连接,操作'1


当然,我可以删除ID并自动添加。但是我想手动添加它,如果它为空,则自动生成。

首先,您的ID列将永远不会为空,因为您使用的是int类型,而不是可为空的int类型

通过将
public int-ID{get;set;}
更改为
public int-ID{get;set;}

您收到此错误的原因是数据库上的
IDENTITY\u INSERT
设置为off

如果将以下属性/约定应用于类中的属性并更新对数据库的更改,您应该会发现允许插入到标识列中

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; }    
} 
或者,您也可以使用OnModelCreating重载

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Worker>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().Property(x=>x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}

首先,您的ID列永远不会为null,因为您使用的是int类型,而不是可为null的int类型

通过将
public int-ID{get;set;}
更改为
public int-ID{get;set;}

您收到此错误的原因是数据库上的
IDENTITY\u INSERT
设置为off

如果将以下属性/约定应用于类中的属性并更新对数据库的更改,您应该会发现允许插入到标识列中

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; }    
} 
或者,您也可以使用OnModelCreating重载

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Worker>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().Property(x=>x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}

谢谢。我知道“?”符号并会尝试它。但如果不是我添加的,我需要自动生成ID。我将Worker和WorkErasignment重新制作为int?但我有if(!WorkerExists(Worker.ID)){return NotFound();}最好将if(!WorkerExists(Worker.ID))更改为if(!Worker.ID.HasValue | | |!WorkerExists(Worker.ID.Value))“无法插入显式值”如果仅添加“?”符号,则存在错误。我知道“?”符号,并将尝试该符号。但如果不是我添加的,则需要自动生成ID。我将Worker和WorkerAssignment重新制作为int?但我有if(!WorkerExists(Worker.ID)){return NotFound();}最好将if(!WorkerExists(Worker.ID))更改为if(!Worker.ID.HasValue | | |!WorkerExists(Worker.ID.Value))“无法插入显式值”如果仅添加“?”符号,则存在错误