1

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?

3
  • Where your u variable is coming from? Dec 19, 2011 at 19:05
  • u is a session variable.
    – user496607
    Dec 19, 2011 at 19:15
  • OK but is it an entity object? Dec 19, 2011 at 19:20

2 Answers 2

1

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.

3
  • This suggestion fixes the problem. However, a new problem occurs with adding the user: An entity object cannot be referenced by multiple instances of IEntityChangeTracker. If I move the user to be added after wpe.AddToPost(), I get the same error as previously.
    – user496607
    Dec 19, 2011 at 19:15
  • This solved the problem. The only issue was with the User which was retrieved from the Session and therefore I wasnt able to retrieve its context. To solve this, the sign in page was changed so it retrieved users with MergeOption.NoTracking. Thank you very much for your suggestions, it really spared a lot of my time.
    – user496607
    Dec 19, 2011 at 19:41
  • No problem. For your next project it might be worth looking at EF4.1 as it has support for POCO classes without tracking. That presents a different set of problems and a bit more manual coding, but we are using it successfully on a large scale project and I find it much easier to use for WEB scenarios. Especially considering the WEB is stateless and you can not track changes across postbacks anyway (without doing some nasty data context sharing stuff which is a big nono).. Key being next project: it's got a steep learning curve :)
    – Gats
    Dec 19, 2011 at 19:44
1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.