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

Asked

Viewed 108 times

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 different context than Locker. An exception is always made when I try to retrieve a Lockler, because he can’t get the list from Lockerreserve.

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

    return lockers.FirstOrDefault();
}

It is possible for a class to have a list of a type that is not in its context in the Entity Framework ?

  • I don’t understand exactly what you want to do... Puts the code from where the exception is being thrown, I did not understand what would be recover in its context

  • I put the method where the exception is thrown. When he goes to pick up a Locker from context gives error, as Locker has a list of Lockerreserve that belongs to another context. And I can’t put Lockerreserve in the same context as Locker, because it will also be accessed in other contexts.

  • And how is the Locker object mapped to the bank? Because he doesn’t invent that Locker has a Lockerreserve, or you mapped it in hand or he created the context based on the bank. How’s that?

  • I did the mapping by the same code. Locker has a collection of Lockerreserve and Lockerreserve has a Locker Id.

  • And the Lockerreserve property is pointing to which table? Or is it pointing to nothing? I’m asking this lot of questions because the way to play one inside the other is by apend, If in context 1 the object does not exist then there is no way to apend it, understand?

1 answer

1

It is possible for a class to have a list of a type that is not in its context in the Entity Framework?

No. Almost certain it won’t work.

The importance of having all entities mapped in just one context is exactly to ensure cohesion between entities. Separating, you can’t tell the context that that relationship exists, so the note of the exception is correct.

I don’t 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).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.