Property of a class that belongs to another context in the Entity Framework

3

I have a class called Locker that has the following property:

public virtual ICollection<LockerReserve> Reserves { get; set; }

The problem is that LockerReserve belongs to a context other than Locker. An exception is thrown whenever I try to retrieve a locker because it can not get the LockerReserve list.

public Locker GetLockerById(int lockerId)
{
    var lockers = _lockerContext.Lockers
        .Where(b => b.LockerId == lockerId);

    return lockers.FirstOrDefault();
}

Can a class have a list of a type that is not in its context in the Entity Framework?

    
asked by anonymous 30.11.2015 / 13:29

1 answer

1
  

Can a class have a list of a type that is not in its context in the Entity Framework?

No. Almost certainly that will not work.

The importance of having all mapped entities in just one context is precisely to ensure cohesion between entities. By separating, you can not tell the context that that relationship exists, so the exception note is correct.

I do not know why you want this separation, but there are better ways to solve the problem. I just need more detail in your question to improve this answer (what is the reason for the separation, what are the aspects of this implementation that need to be considered, among others).

    
01.12.2015 / 14:46