【问题标题】:Entity Framework 4.x and Layer SupertypeEntity Framework 4.x 和层超类型
【发布时间】:2012-02-18 09:02:46
【问题描述】:

我想在使用 EF 4.x 时实现层超类型模式((PoEAA)。

假设我有一个名为 Entity 的层超类型类,从该类继承了两个类 Teacher 和 Student。

Person类是这样定义的

class Entity
{
    public int Id {get;set;}
}

还有这样的老师和学生

class Teacher : Entity
{
     publix string Name {get;set;}
}

class Student : Entity
{
     public int Age {get;set;}
}

如何配置 EF 4.x 以便在我的数据库中只有两个对应于 Teacher 和 Student 的表?我尝试使用 TPC 继承策略来映射此结构,但它不适合,它为每个具体类创建三个表..

使用 NHibernate,这种情况很常见并且处理得很好,我的意思是如果我只为 Person 和 Student 创建映射,数据库将只有两个表,我不必显式地实现任何继承。

感谢大家的建议

丽安娜

【问题讨论】:

    标签: entity-framework-4


    【解决方案1】:

    使Entity 类抽象

    public abstract class Entity
    {
        public int Id {get;set;}
    }
    
    public class Teacher : Entity
    {
         publix string Name {get;set;}
    }
    
    public class Student : Entity
    {
         public int Age {get;set;}
    }
    

    【讨论】:

    • 您好,谢谢您的回答。如您所说,使实体类“抽象”并使用 TPC 是解决方案!我首先尝试了这种方法,但它不起作用,EF 说它找不到 Key 属性,这实际上是我问题的重点。 EF 没有找到 key 属性,因为我为 Entity 类创建了一个私有集......它适用于 NHibernate 而不是 EF。无论如何,谢谢你的回答!
    猜你喜欢
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    相关资源
    最近更新 更多