当我执行下面的脚本时,我有以下错误。错误是关于什么,如何解决?
Insert table(OperationID,OpDescription,FilterID)
values (20,'Hierachy Update',1)
错误:
服务器:Msg 544,级别16,状态1,线路1 当IDENTITY_INSERT设置为OFF时,无法为表'table'中的标识列插入显式值。
当我执行下面的脚本时,我有以下错误。错误是关于什么,如何解决?
Insert table(OperationID,OpDescription,FilterID)
values (20,'Hierachy Update',1)
错误:
服务器:Msg 544,级别16,状态1,线路1 当IDENTITY_INSERT设置为OFF时,无法为表'table'中的标识列插入显式值。
当前回答
如果您使用Interface并以通用方式实现savechanges方法,则会出现使用非类型化DBContext或DBSet的问题
如果这是您的情况,我建议使用强类型的dbcontext
MyDBContext.MyEntity.Add(mynewObject)
那么,保存更改就可以了
其他回答
要非常小心地将IDENTITY_INSERT设置为ON。这是一种糟糕的实践,除非数据库处于维护模式并设置为单用户。这不仅会影响您的插入,还会影响试图访问表的其他人的插入。
为什么要在单位字段中输入一个值?
每个人都评论SQL,但是在EntityFramework中发生了什么?我看了整篇文章,没人能解出EF。所以几天后,我找到了一个解决方案: 在EF Core的上下文中创建模型有这样一条指令:modelBuilder。实体<客户端>(实体=>{实体。属性(e => e.d id).ValueGeneratedNever();
这也会产生错误,解决方案:你必须通过ValueGeneratedOnAdd()改变它的工作!
设置该字段的身份增量时,不能在“OperationID”列中插入数据。
不要在该字段中输入值,它将自动设置。
Insert table(OpDescription,FilterID)
values ('Hierachy Update',1)
我使用asp.net core 5.0,我得到这个错误。我得到这个错误,因为我添加了另一个数据并触发了另一个.SaveChanges()方法,如下所示:
_unitOfWorkVval.RepositoryVariantValue.Create(variantValue);
int request = HttpContext.Response.StatusCode;
if (request == 200)
{
int tryCatch = _unitOfWorkCVar.Complete();
if (tryCatch != 0)
{
productVariant.CategoryVariantID = variantValue.CategoryVariantID;
productVariant.ProductID = variantValue.ProductID;
productVariant.CreatedDate = DateTime.Now;
_unitOfWorkProductVariant.RepositoryProductVariant.Create(productVariant);
_unitOfWorkVval.RepositoryVariantValue.Create(variantValue);
int request2 = HttpContext.Response.StatusCode;
if(request==200)
{
int tryCatch2=_unitOfWorkProductVariant.Complete();//The point where i get that error
}///.......
This occurs when you have a (Primary key) column that is not set to Is Identity to true in SQL and you don't pass explicit value thereof during insert. It will take the first row, then you wont be able to insert the second row, the error will pop up. This can be corrected by adding this line of code [DatabaseGenerated(DatabaseGeneratedOption.Identity)] in your PrimaryKey column and make sure its set to a data type int. If the column is the primary key and is set to IsIDentity to true in SQL there is no need for this line of code [DatabaseGenerated(DatabaseGeneratedOption.Identity)] this also occurs when u have a column that is not the primary key, in SQL that is set to Is Identity to true, and in your EF you did not add this line of code [DatabaseGenerated(DatabaseGeneratedOption.Identity)]