如何保存与现有实体有父关系的新实体?

发布于 2024-12-20 02:05:30 字数 1575 浏览 2 评论 0原文

如果我运行下面的代码,我会得到一个异常。

为了更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望她远 2024-12-27 02:05:30

如果您使用 poco 对象,问题将出在您创建 ClientUser 的方式上。您需要在当前上下文中创建新的 ClientUser:

ClientUser client = _myObjectContext.CreateObject<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:

ClientUser client = _myObjectContext.CreateObject<ClientUser>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文