为什么LINQ to SQL实体关联在插入新记录时会创建一个新的(重复)行?

时间:2022-05-02 10:38:38

I am trying to insert a new entity using LINQ-to-SQL, and entity is associated with a User entity. The insert of the new entity is successful, but my existing User entity gets inserted as if it were a new User. The code looks something like the following:

我正在尝试使用LINQ-to-SQL插入新实体,并且实体与User实体相关联。新实体的插入成功,但我现有的用户实体被插入,就像它是一个新用户一样。代码如下所示:

var someEntity = new Entity();
someEntity.User = this.User;
dataContextInstance.SomeEntities.InsertOnSubmit(someEntity);
dataContextInstance.SubmitChanges();

Does anyone know why the user is being inserted as a brand new entity into the Users table? It would seem that the User.UserId would become the foreign key value in the UserId column of the row mapped to the someEntity that is being inserted.

有谁知道为什么用户被插入到Users表中作为一个全新的实体?似乎User.UserId将成为映射到正在插入的someEntity的行的UserId列中的外键值。

Thanks for any help/suggestions/comments

感谢您的任何帮助/建议/意见

3 个解决方案

#1


9  

Since the User entity has been previously loaded by another DataContext (which should hopefully be disposed by now!), you have to attach it to the new (current) DataContext otherwise the DataContext will view it as a new Entity and not an existing one (which already exists in the DB).

由于User实体先前已被另一个DataContext加载(希望现在可以处理它!),您必须将它附加到新的(当前)DataContext,否则DataContext将其视为新实体而不是现有实体(它已经存在于DB中。

#2


0  

Make sure you're using the same instance of DataContext for both tables. See my question here for (I think) a clearer explanation of the problem.

确保您为两个表使用相同的DataContext实例。在这里看到我的问题(我认为)更清楚地解释了这个问题。

#3


-1  

Sorry, I don't get it? Do you want to update your existing user? What is someEntity for?

对不起,我收不到了吗?您想要更新现有用户吗?什么是一些实力?

It makes sense, that LINQ tries to Insert a New user, because you tell it to do so. If you just want to change one of your users you need to select him out of SomeEntities, do the update and then call SubmitChanges (LINQ will recognize, that the original entity has been modified) - without .InsertOnSubmit.

LINQ尝试插入一个新用户是有意义的,因为你告诉它这样做。如果您只想更改一个用户,则需要从SomeEntities中选择他,执行更新,然后调用SubmitChanges(LINQ将识别,原始实体已被修改) - 没有.InsertOnSubmit。

#1


9  

Since the User entity has been previously loaded by another DataContext (which should hopefully be disposed by now!), you have to attach it to the new (current) DataContext otherwise the DataContext will view it as a new Entity and not an existing one (which already exists in the DB).

由于User实体先前已被另一个DataContext加载(希望现在可以处理它!),您必须将它附加到新的(当前)DataContext,否则DataContext将其视为新实体而不是现有实体(它已经存在于DB中。

#2


0  

Make sure you're using the same instance of DataContext for both tables. See my question here for (I think) a clearer explanation of the problem.

确保您为两个表使用相同的DataContext实例。在这里看到我的问题(我认为)更清楚地解释了这个问题。

#3


-1  

Sorry, I don't get it? Do you want to update your existing user? What is someEntity for?

对不起,我收不到了吗?您想要更新现有用户吗?什么是一些实力?

It makes sense, that LINQ tries to Insert a New user, because you tell it to do so. If you just want to change one of your users you need to select him out of SomeEntities, do the update and then call SubmitChanges (LINQ will recognize, that the original entity has been modified) - without .InsertOnSubmit.

LINQ尝试插入一个新用户是有意义的,因为你告诉它这样做。如果您只想更改一个用户,则需要从SomeEntities中选择他,执行更新,然后调用SubmitChanges(LINQ将识别,原始实体已被修改) - 没有.InsertOnSubmit。