实体框架(无法定义两个对象之间的关系)

发布于 2024-12-22 02:57:22 字数 1024 浏览 1 评论 0原文

我试图在 ASP .Net 网页中执行以下代码:

  using (var wpe = new CL40215_wpnEntities())
                {
                int cid = Convert.ToInt32(cmbCity.SelectedItem.Value);
                int catid = Convert.ToInt32(cmbCategory.SelectedValue);

             Post p = new Post
             {
                 title = txtTitle.Text,
                 description = txtDescription.Text,
                 User = u,
                 City = wpe.Cities.First(c => c.id == cid),
                 initialprice = 0,
                 finalprice = 10,
                 Category = wpe.Categories.First(c => c.id == catid),
                 postdate = DateTime.Now,
                 closedate = DateTime.Now.AddDays(Convert.ToInt32(cmbDays.SelectedValue)),
                currentprice = 1
             };
                wpe.AddToPosts(p);
                wpe.SaveChanges();
 }

但是,我收到以下错误:

无法定义两个对象之间的关系,因为它们附加到不同的 ObjectContext 对象。

有谁知道为什么会发生这种情况以及如何解决这个问题?

I am trying to execute the following code in a ASP .Net web page:

  using (var wpe = new CL40215_wpnEntities())
                {
                int cid = Convert.ToInt32(cmbCity.SelectedItem.Value);
                int catid = Convert.ToInt32(cmbCategory.SelectedValue);

             Post p = new Post
             {
                 title = txtTitle.Text,
                 description = txtDescription.Text,
                 User = u,
                 City = wpe.Cities.First(c => c.id == cid),
                 initialprice = 0,
                 finalprice = 10,
                 Category = wpe.Categories.First(c => c.id == catid),
                 postdate = DateTime.Now,
                 closedate = DateTime.Now.AddDays(Convert.ToInt32(cmbDays.SelectedValue)),
                currentprice = 1
             };
                wpe.AddToPosts(p);
                wpe.SaveChanges();
 }

However, I get the following error:

The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.

Does anyone have any idea why is this happening, and how to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

满地尘埃落定 2024-12-29 02:57:22

尝试更改对象初始化以首先附加帖子对象,然后创建关系。

    var category = wpe.Categories.First(c => c.id == catid);
    var city = wpe.Cities.First(c => c.id == cid);
    var user = wpe.Users.First(newU => newU.Id == u.Id);
    Post p = new Post
    {
        title = txtTitle.Text,
        description = txtDescription.Text,
        initialprice = 0,
        finalprice = 10,
        postdate = DateTime.Now,
        closedate = DateTime.Now.AddDays(Convert.ToInt32(cmbDays.SelectedValue)),
        currentprice = 1
    };
    wpe.AddToPosts(p);
    p.City = city;
    p.Category = category;
    p.User = user;

    wpe.SaveChanges();

如果您使用持久性感知类,则它们需要在创建关系之前相互了解。这意味着您需要在构建对象之前分离子对象,然后首先附加所有子对象,或者如上所述,首先附加新帖子。不确定您的 addtoposts 存储库调用正在做什么,但如果您在对象有效之前没有运行保存更改,那么这应该可以工作。

编辑:抱歉。我没有注意到添加了用户对象。您需要使用当前上下文重新加载用户,或者将用户从之前的上下文中分离出来并将其附加到当前上下文。

Try changing your object initialisation to attach the post object first then create the relationship.

    var category = wpe.Categories.First(c => c.id == catid);
    var city = wpe.Cities.First(c => c.id == cid);
    var user = wpe.Users.First(newU => newU.Id == u.Id);
    Post p = new Post
    {
        title = txtTitle.Text,
        description = txtDescription.Text,
        initialprice = 0,
        finalprice = 10,
        postdate = DateTime.Now,
        closedate = DateTime.Now.AddDays(Convert.ToInt32(cmbDays.SelectedValue)),
        currentprice = 1
    };
    wpe.AddToPosts(p);
    p.City = city;
    p.Category = category;
    p.User = user;

    wpe.SaveChanges();

If you are using persistence aware classes, they need to know about each other before the relationship can be created. That means you will either need to detach the children before building the object, then attach them all children first, or, as above, attach the new post first. Not sure what youre addtoposts repo call is doing, but that should work provided you aren't running a save changes before the object is valid.

EDIT: Apologies. I didn't notice the user object being added. You will need to either reload the user with the current context or DETACH the user from it's previous context and ATTACH it to the current one.

安静 2024-12-29 02:57:22

可能的问题之一是 u 引用了 User。我不知道它来自哪里,而且 u 确实有可能来自不同的上下文。

One of the possible problems is u referring to User. I don't see where it comes from and it indeed could be possible that u comes from a different context.

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