Hi i have the following code

  public void allocateUserRole(CommonDB.user user, CommonDB.role role)
             {
                 user.roles.Add(role);
                 this.Entities.SaveChanges();
             }

It is giving me the following error because i am trying to insert the userRoles. Where tblUser haas an intermediary table with tblRole

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

Member Avatar for LastMitch

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

The error is on this line:

user.roles.Add(role);

Try this:

user.roles.Add(user);
role.roles.Add(role);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.