Canam
Canam

Reputation: 299

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

I have this code (VS2010 ASP.NET MVC 3 with EF 4):

Project project = new Project();
project.Number = number;
project.Name = name;
context.AddObject(project);

ProjectUser projectUser = new ProjectUser();
projectUser.User = user;
projectUser.Status = 1;
project.ProjectUsers.Add(projectUser);

context.SaveChanges(true);

It generates the following error (on the "project.ProjectUsers.Add(projectUser)" line)

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

I don't understand why cause, as far as I know, both objects are using the same ObjectContext (but I'm new to EF).

What am I doing wrong? Thanks for your help!

Upvotes: 6

Views: 16925

Answers (5)

Synctrex
Synctrex

Reputation: 881

I know this is old as dirt, but I spent several hours being frustrated by this unhelpful error today, and Babacar's answer finally pointed me in the right direction.

With Ninject managing dependencies, I needed to specify

kernel.Bind<Context>().ToSelf().InSingletonScope();

before my EF calls between the UserManager and various controllers started behaving properly.

Upvotes: 0

I think a good way to avoid this problem is to implement your application database context as a singleton. Here is a sample for you:

here your ApplicationDbContext.cs `java

public class ApplicationDbContext : DbContext, IDbContextFactory<DbContext>
{
    protected static ApplicationDbContext _instance { private set; get; }

    private ApplicationDbContext() : base("ApplicationDbContext")
    {
    }

    public DbContext Create()
    {
        return getInstance();
    }


    public static ApplicationDbContext getInstance()
    {
        if (_instance == null) {
            _instance = new ApplicationDbContext();
        }
        return _instance;
    }
}

`

here your controller `java

private ApplicationDbContext _db;

public class HelloController : Controller
{
    _db = ApplicationDbContext.getInstance();
}

`

so you can have the exact same instance no matter where you are in you app

Upvotes: 0

Joe Enos
Joe Enos

Reputation: 40393

If your user variable is an entity type, and it is assigned to a different context, then you'd experience this problem.

I don't think the problem is between your Project and ProjectUser objects, only because your ProjectUser object isn't explicitly assigned to a context - I think by default it will go to the same context as the Project when you go to save it.

I believe you get this error only when you truly have two contexts and try to join them together.

Upvotes: 8

Jeff
Jeff

Reputation: 36573

You shouldn't need this line at all:

project.ProjectUsers.Add(projectUser);

Just adding the project should be sufficient, because you're setting the relationship.

context.AddObject(project);

Upvotes: 0

TRayburn
TRayburn

Reputation: 1535

Just list you did for Project, you need to add the ProjectUser to the context. So mimic the line:

context.AddObject(project);

And instead make it

context.AddObject(projectUser);

And do that before you add it to the collection on project.

Upvotes: 4

Related Questions