Why is this Exception?- The relationship between the two objects cannot be defined because they are

Posted by dev-1787 on Stack Overflow See other posts from Stack Overflow or by dev-1787
Published on 2010-04-19T10:27:50Z Indexed on 2010/04/19 10:33 UTC
Read the original article Hit count: 1731

Filed under:
|
|

I m getting this Exception-"The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects."

I ve user table and country table. The countryid is referred in user table.

I am getting the above Exception when I am trying to add entry in user table.

This is my code-

using

(MyContext _db = new MyContext ())

{

User user = User .CreateUser(0, Name, address, city, 0, 0, email, zip);

Country country = _db.Country.Where("it.Id=@Id", new ObjectParameter("Id",countryId)).First();

user.Country = country;

State state = _db.State.Where("it.Id=@Id", new ObjectParameter("Id", stateId)).First();

user.State = state;

_db.AddToUser(user );//Here I am getting that Exception

_db.SaveChanges();

}

© Stack Overflow or respective owner

Related posts about ado

Related posts about entity