简体   繁体   中英

Inheritance between two contexts in Entity Framework

I have one edmx in one dll, and need to have an entity in an edmx in another dll inherit from an entity in the first edmx. I have attempted to extend the initial context of the first edmx with the second with no success. What is the best way to accomplish this?

That is not possible. One EDMX = one ObjectContext and no inheritance among them. I found a special hack how to force context to load multiple EDMXs but they must be in the same assembly and it works only for cross EDMX linq-to-entities queries.

I think you must model whole inheritance hierarchy again in the second EDMX and reuse same POCO class for the parent = parent entity must be in both EDMXs. Check these articles about working with multiple models ( part 1 , part 2 ). There is possibility to reusing CSDL types from one EDMX in other EDMX for defining associations but it will not work for inheritance because inheritance is defined in MSL which cannot be reused.

Inheritance may not be the best solution for this. I would suggest dependency injection from both entities from separate assemblies, eg:

public class CompositeObj
{
    protected ObjectType1 obj1 { get; set; }
    protected ObjectType2 obj2 { get; set; }

    public CompositeObj(ObjectType1 obj1, ObjectType2 obj2)
    {
         this.obj1 = obj1;
         this.obj2 = obj2;
    }

    public string Property1 { get { return this.obj1.Property1; } }
    public string Property2 { get { return this.obj2.Property2; } }
    pulbic string Property3 { get { return this.obj1.Property1 + this.obj2.Property2; } }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM