如何保存与现有实体有父关系的新实体?
如果我运行下面的代码,我会得到一个异常。
为了更新 AssociationSet“x”,EntitySet“x”中的相应实体必须在 ObjectStateManager 中可用。
这是我的代码。无论我是否将客户端添加到用户实体集中,我仍然收到错误。
MyDBEntities db = new MyDBEntities();
//create new client entity
ClientUser client = new ClientUser();
client.UID = 9;
client.UserName = "KKL";
client.CompanyName = "KKL Company";
//retrieve existing entity
AdminUser admin = db.Users.OfType<AdminUser>().FirstOrDefault();
//assign AdminUser of new client to existing admin
client.AdminUser = admin;
//db.Users.AddObject(client);
db.SaveChanges();
我尝试先保存新实体,然后添加关系并再次保存。但它显示相同的错误消息。 :(
MyDBEntities db = new MyDBEntities();
//create new client entity
ClientUser client = new ClientUser();
client.UID = 9;
client.UserName = "KKL";
client.CompanyName = "KKL Company";
//first save the new object
db.Users.AddObject(client);
db.SaveChanges();
//retrieve existing entity
AdminUser admin = db.Users.OfType<AdminUser>().FirstOrDefault();
client.AdminUser = admin;
db.SaveChanges();
解决!!!
我的问题是关联。
我在 ClientUser 和 AdminUser 之间有 0..1 关系。
Admin (0..1) <-- >客户端 (0..1)
我已经将该管理员用户分配给另一个现有客户端,然后我尝试创建新客户端并分配该管理员,它引发了错误,
我通过将关系更改为来修复它 。下面
Admin (0..1) < --> 客户 (*)
If I run the code below, I got an exception.
In order to update the AssociationSet 'x', the corresponding entity from EntitySet 'x' must be available in the ObjectStateManager.
Here is my code. No matter I add the client to the Users entityset or not, I still get the error.
MyDBEntities db = new MyDBEntities();
//create new client entity
ClientUser client = new ClientUser();
client.UID = 9;
client.UserName = "KKL";
client.CompanyName = "KKL Company";
//retrieve existing entity
AdminUser admin = db.Users.OfType<AdminUser>().FirstOrDefault();
//assign AdminUser of new client to existing admin
client.AdminUser = admin;
//db.Users.AddObject(client);
db.SaveChanges();
I tried to save the New Entity first, then add relation and save again. But it's showing same error message. :(
MyDBEntities db = new MyDBEntities();
//create new client entity
ClientUser client = new ClientUser();
client.UID = 9;
client.UserName = "KKL";
client.CompanyName = "KKL Company";
//first save the new object
db.Users.AddObject(client);
db.SaveChanges();
//retrieve existing entity
AdminUser admin = db.Users.OfType<AdminUser>().FirstOrDefault();
client.AdminUser = admin;
db.SaveChanges();
SOLVE!!!
My problem is in association.
I have 0..1 relationship at both ends between ClientUser and AdminUser.
Admin (0..1) < -- > Client (0..1)
And I've already assigned that admin user to another existing client. Then I tried to create new client and assign that admin and it raise the errors.
I fixed it by changing the relationship as below
Admin (0..1) < -- > Client (*)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 poco 对象,问题将出在您创建 ClientUser 的方式上。您需要在当前上下文中创建新的 ClientUser:
If you're using poco objects the problem will be with the way that you've created the ClientUser. You need to create the new ClientUser within the current context: