我创建了一个新对象并希望将其附加到这样的上下文中,
User user = new User();
user.userName=”Kobe”;
context.Attach(user);
出现一条错误消息——“无法将具有空 EntityKey 值的对象附加到对象上下文”。如果我从数据库中查询出一个用户对象并将其 EntityKey 分配给新对象,然后像这样分离查询结果对象,
User user = (from u in context.Users where u.userID == 1 select u).First();
User newUser = new User();
newUser.userName = “Kobe”;
newUser.EntityKey = user.EntityKey;
context.Detach(user);
context.Attach(newUser);
出现另一条错误消息 - “无法附加对象,因为作为 EntityKey 一部分的属性的值与 EntityKey 中的相应值不匹配。” 我真的不知道EntityKey是什么,我在网上搜索并在MSDN中看到了EntityKey Class,但仍然无法理解。当 EntityKey 创建并附加到对象时?我在哪里可以找到它?如果我分离对象,为什么 EntityKey 仍然存在?
任何人都可以帮忙吗?提前致谢!