Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to relate objects from multiple contexts using the Entity Framework

I am very new to the entity framework, so please bear with me...

How can I relate two objects from different contexts together?

The example below throws the following exception:

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

void MyFunction()
{
    using (TCPSEntities model = new TCPSEntities())
    {
        EmployeeRoles er = model.EmployeeRoles.First(p=>p.EmployeeId == 123);
        er.Roles = GetDefaultRole();
        model.SaveChanges();
     }
}

private static Roles GetDefaultRole()
{
    Roles r = null;
    using (TCPSEntities model = new TCPSEntities())
    {
        r = model.Roles.First(p => p.RoleId == 1);
    }
    return r;
}

Using one context is not an option because we are using the EF in an ASP.NET application.

like image 239
Giovanni Galbo Avatar asked Oct 01 '08 18:10

Giovanni Galbo


3 Answers

You will have to use the same context (you can pass the context to the getdefaultrole method) or rethink the relationships and extend the entity.

EDIT: Wanted to add this was for the example provided, using asp.net will require you to fully think out your context and relationship designs.

You could simply pass the context.. IE:

void MyFunction()
{
    using (TCPSEntities model = new TCPSEntities())
    {
        EmployeeRoles er = model.EmployeeRoles.First(p=>p.EmployeeId == 123);
        er.Roles = GetDefaultRole(model);
        model.SaveChanges();
     }

}

private static Roles GetDefaultRole(TCPSEntities model)
{
    Roles r = null;
    r = model.Roles.First(p => p.RoleId == 1);
    return r;
}
like image 135
Quintin Robinson Avatar answered Oct 31 '22 12:10

Quintin Robinson


Another approach that you could use here is to detach objects from one context, and then attach them to another context. That's a bit of a hack, and it may not work in your situation, but it might be an option.

    public void GuestUserTest()
    {
        SlideLincEntities ctx1 = new SlideLincEntities();
        GuestUser user = GuestUser.CreateGuestUser();
        user.UserName = "Something";
        ctx1.AddToUser(user);
        ctx1.SaveChanges();

        SlideLincEntities ctx2 = new SlideLincEntities();
        ctx1.Detach(user);
        user.UserName = "Something Else";
        ctx2.Attach(user);
        ctx2.SaveChanges();
    }
like image 3
Ken Smith Avatar answered Oct 31 '22 14:10

Ken Smith


Yep - working across 2 or more contexts is not supported in V1 of Entity Framework.

Just in case you haven't already found it, there is a good faq on EF at http://blogs.msdn.com/dsimmons/pages/entity-framework-faq.aspx

like image 2
Eric Nelson Avatar answered Oct 31 '22 12:10

Eric Nelson