简体   繁体   English

实体框架中两个上下文之间的 Inheritance

[英]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.我在一个 dll 中有一个 edmx,并且需要在另一个 dll 中的一个 edmx 中有一个实体从第一个 edmx 中的一个实体继承。 I have attempted to extend the initial context of the first edmx with the second with no success.我试图用第二个扩展第一个 edmx 的初始上下文,但没有成功。 What is the best way to accomplish this?实现这一目标的最佳方法是什么?

That is not possible.这是不可能的。 One EDMX = one ObjectContext and no inheritance among them.一个 EDMX = 一个ObjectContext ,其中没有 inheritance。 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.我发现了一个特殊的技巧,如何强制上下文加载多个 EDMX,但它们必须在同一个程序集中,并且它仅适用于跨 EDMX linq-to-entities 查询。

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.我认为您必须在第二个 EDMX 中再次使用 model 整个 inheritance 层次结构,并为父实体重用相同的 POCO class = 父实体必须在两个 EDMX 中。 Check these articles about working with multiple models ( part 1 , part 2 ).查看有关使用多个模型的这些文章( 第 1 部分,第 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.有可能在其他 EDMX 中重用来自一个 EDMX 的 CSDL 类型来定义关联,但它不适用于 inheritance,因为 inheritance 在 MSL 中定义,无法重用。

Inheritance may not be the best solution for this. Inheritance 可能不是最好的解决方案。 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; } }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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