简体   繁体   English

在Entity Framework中更新关系会产生SQL错误

[英]Updating a relationship in Entity Framework generates SQL error

I'm getting headaches here. 我在这里头疼。 I've got a one to zero-or-one relationship between a Session and a User object, designed in Entity Framework 4 using the model first approach. 我在Session和User对象之间建立了一对零或一对一的关系,该关系是在Entity Framework 4中使用模型优先方法设计的。

  • A User should have a session ( one ) 一个用户应该有一个会话( 一个
  • A Session can have a User ( zero-to-one ). 会话可以有一个用户( 零对一 )。

So, in runtime I pull a session from the database using 因此,在运行时,我使用

Session session = context.Sessions.FirstOrDefault(s => s.Token.Equals(token));

and after that I create a user, created based on user input like this 然后我创建一个用户,根据这样的用户输入创建

user = context.Users.Create();

user.GUID = Guid.NewGuid();
user.Username = username;
user.Firstname = firstname;
user.Lastname = lastname;
user.Session = session;

context.Users.Add(user);
context.SaveChanges();

and when I save, the SQL server throws an exception telling me that the GUID I try to insert in the database already exists, because yeah, it does. 当我保存时,SQL Server引发异常,告诉我我尝试插入数据库中的GUID已经存在,因为是的,确实如此。 The problem is, that it shouldnt insert, it should update the session and fill the forein key that belongs with it. 问题是,它不应该插入,它应该更新会话并填充它所属的forein键。 The foreign key resides on the User side of the relation, this is alright, right? 外键位于关系的用户端,这样可以吗?

edit The user should be a new user. 编辑用户为新用户。 Updating the user is not what I'm trying to accomplish. 更新用户不是我要完成的工作。 Also, the error is because the code tries to insert the session with the same primary key value, instead of just updating it's foreing keys. 同样,该错误是因为代码试图插入具有相同主键值的会话,而不仅仅是更新它的前键。

Here's a chronological list of what should be done: 这是应该执行的时间顺序列表:

  • Session gets created 会话已创建
  • Session gets saved 会话已保存
  • Session gets selected from the database 从数据库中选择会话
  • User gets created 用户被创建
  • User <-> Session relations gets set 用户<->会话关系已设置
  • User gets saved 用户被保存

And what I get is: 我得到的是:

Violation of PRIMARY KEY constraint 'PK_Sessions'. 违反PRIMARY KEY约束“ PK_Sessions”。 Cannot insert duplicate key in object 'dbo.Sessions'. 无法在对象“ dbo.Sessions”中插入重复密钥。 The duplicate key value is VALUE . 重复的键值为VALUE The statement has been terminated. 该语句已终止。

Please help me out, I've been fiddling with this for the past 2 hours. 请帮帮我,过去2个小时我一直在摆弄。

Edit: Changed my answer to reflect changed problem description :) 编辑:更改了我的答案以反映更改后的问题描述:)

Make sure you always use the same context instance when handling these kind of updates. 确保在处理此类更新时始终使用相同的上下文实例。

What seemed to be happening here was that session was retrieved in one context, but the user was saved in another instance of that context. 这里似乎正在发生的事情是,在一个上下文中检索了会话,但是将用户保存在该上下文的另一个实例中。 Therefore, in the new context, the session was seen as new and hence, it was insterted (wrongfully) 因此,在新的背景下,该会议被视为是新会议,因此被(错误地)插入。

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

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