当我执行下面的脚本时,我有以下错误。错误是关于什么,如何解决?
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'中的标识列插入显式值。
当前回答
我通过每次向数据库添加任何东西时创建一个新对象来解决这个问题。
其他回答
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)]
每个人都评论SQL,但是在EntityFramework中发生了什么?我看了整篇文章,没人能解出EF。所以几天后,我找到了一个解决方案: 在EF Core的上下文中创建模型有这样一条指令:modelBuilder。实体<客户端>(实体=>{实体。属性(e => e.d id).ValueGeneratedNever();
这也会产生错误,解决方案:你必须通过ValueGeneratedOnAdd()改变它的工作!
首先…
首先,您需要知道为什么会出现错误。
让我们以一个简单的JSON数据HttpPost为例,如下所示:
{
"conversationID": 1,
"senderUserID": 1,
"receiverUserID": 2,
"message": "I fell for the wrong man!",
"created":"2022-02-14T21:18:11.186Z"
}
如果你在连接到SQLServer或任何其他数据库服务器时使用Entity框架核心生成数据库,数据库将自动负责更新和自动生成标识列的Key/Unique标识符,在大多数情况下,这是一个由它自动递增的整数值。
为了使用内置的约定安全地发布数据,可以将ID字段从想要发送到数据库的数据中删除,并让数据库引擎和ef-core完成它们设计的繁重工作。
所以发布数据的正确方式是:
{
"senderUserID": 1,
"receiverUserID": 2,
"message": "I fell for the wrong man!",
"created":"2022-02-14T21:18:11.186Z"
}
你会注意到我拿出了ConversationID。
你可以在这个网站上了解更多关于实体框架的信息:https://entityframeworkcore.com
我希望您在应用程序中更多地遵循约定,而不是配置。知道如何按照已经被证实的标准和惯例做事将为您节省大量的工作时间。
请注意,如果您用;结束每一行,SET IDENTITY_INSERT mytable ON命令将不适用于以下行。
即。 像这样的查询
SET IDENTITY_INSERT mytable ON;
INSERT INTO mytable (VoucherID, name) VALUES (1, 'Cole');
给出错误 当IDENTITY_INSERT设置为OFF时,无法为表'mytable'中的身份列插入显式值。
但是像这样的查询是可以的:
SET IDENTITY_INSERT mytable ON
INSERT INTO mytable (VoucherID, name) VALUES (1, 'Cole')
SET IDENTITY_INSERT mytable OFF;
SET IDENTITY_INSERT命令似乎只适用于事务,而;将表示事务的结束。
EF Core 3.x
引用Leniel Maccaferri,我有一个带有自动递增属性的表,称为ID(原始主键)和另一个属性称为Other_ID(新的主键)。最初ID是主键,但后来Other_ID需要成为新的主键。由于ID正在应用程序的其他部分使用,我不能只是从表中删除它。Leniel Maccaferri解决方案只适用于我之后,我添加了以下片段:
entity.HasKey(x => x.Other_ID);
entity.Property(x => x.ID).ValueGeneratedOnAdd();
完整代码片段如下(ApplicationDbContext.cs):
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
base.OnModelCreating(builder);
builder.Entity<tablename>(entity =>
{
entity.HasKey(x => x.Other_ID);
entity.Property(x => x.ID).ValueGeneratedOnAdd();
entity.HasAlternateKey(x => new { x.Other_ID, x.ID });
});
}