简体   繁体   English

LINQ to SQL:插入子实体失败,因为SQL中不包含ID?

[英]LINQ to SQL: inserting child entity fails because ID is not included in SQL?

I'm doing a very simple insert into a database by assigning the child of an LINQ-to-SQL entity, but it's failing on the foreign key. 我正在通过分配LINQ-to-SQL实体的子代来非常简单地插入数据库,但是在外键上失败了。

The code is: 代码是:

var imageStripFileAltText = new ImageStripFileAltText();
imageStripFileAltText.Alt = "alt text";
imageStripFile.ImageStripFileAltText = imageStripFileAltText;
db.SubmitChanges();

imageStripFile is a custom type ImageStripFile with a corresponding table in the database. imageStripFile是自定义类型ImageStripFile ,在数据库中具有相应的表。
db is a DataContext. db是一个DataContext。

When I inspect imageStripFile.ImageStripFileAltText in the debugger, I see that it has been assigned the correct foreign key Id, that is, the primary key of imageStripFile . 当我在调试器中检查imageStripFile.ImageStripFileAltText时,我看到已为其分配了正确的外键ID,即imageStripFile的主键。 However, I get the following Exception: 但是,出现以下异常:

System.Data.SqlClient.SqlException: 'The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ImageStripFileAltText_ImageStripFile". System.Data.SqlClient.SqlException:'INSERT语句与FOREIGN KEY约束“ FK_ImageStripFileAltText_ImageStripFile”冲突。 The conflict occurred in database "Cms", table "dbo.ImageStripFile", column 'Id'. 数据库“ Cms”的表“ dbo.ImageStripFile”的列“ Id”中发生了冲突。

Since I made sure the ImageStripFile with the corresponding ID exists, this baffled me, until I inspected the generated SQL: 由于我确定具有对应ID的ImageStripFile存在,这使我感到困惑,直到我检查了生成的SQL:

DECLARE @p0 NVarChar(4000) = 'alt text' 
INSERT INTO [dbo].[ImageStripFileAltText]([Alt])
VALUES (@p0)

The generated SQL does not include the ID in the parameters! 生成的SQL在参数中不包含ID!

What am I doing wrong? 我究竟做错了什么?

I've also tried directly assigning the foreign key ID: 我也尝试过直接分配外键ID:

var imageStripFileAltText =
    new ImageStripFileAltText() { ImageStripFileId = imageStripFile.Id };

but the result is the same. 但结果是一样的。

Edit: If I remove the IsIdentity property from the child table's primary key (which I now believe is the correct situation), I get a different Exception: 编辑:如果我从子表的主键中删除IsIdentity属性(我现在认为这是正确的情况),则会得到另一个异常:

System.Data.SqlClient.SqlException: 'Cannot insert the value NULL into column 'ImageStripFileId', table 'Cms.dbo.ImageStripFileAltText'; System.Data.SqlClient.SqlException:'无法将值NULL插入表'Cms.dbo.ImageStripFileAltText'的'ImageStripFileId'列中; column does not allow nulls. 列不允许为空。 INSERT fails. INSERT失败。

Note that this corresponds with the generated SQL as well. 请注意,这也与生成的SQL相对应。

In my scenario, the PK field in my table was not auto incremented ie IsIdentity was set to false. 在我的方案中,表中的PK字段未自动递增,即IsIdentity设置为false。 Try checking this in your tables. 尝试在表格中检查一下。

Wow. 哇。 I was building my model on the Development Database and executing the code on the Production Database, on which the ImageStripFileAltText table had a slightly different scheme (probably something with IsIdentity?), since I was still tinkering with it when I wrote the code. 我正在开发数据库上构建模型,并在生产数据库上执行代码,在该数据库上ImageStripFileAltText表的方案略有不同(可能与IsIdentity有关?),因为在编写代码时我仍在修改它。

Long story short: the code is fine; 长话短说:代码很好; don't be an idiot and work diligently. 不要白痴,要勤奋工作。

IsIdentity需要设置为true并自动递增

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM