实体框架4代码第一流畅的API配置与一对一的继承关系

时间:2011-12-15 02:11:46

标签: entity-framework fluent

我正在尝试使用EF代码首先使用Fluent配置为所有业务类实现“Party”域模式。配置让我喝酒(不是坏事,但下一站是悬崖)

我希望发生的事情类似于下面显示的代码。数据库的要求是我最终得到以下表格:

单独的“缔约方”表 一个单独的“人”表 一个单独的“组织”表

人员表与缔约方表有一对一的关系,即在您有人之前必须有一个缔约方。

组织表与缔约方表有一对一的关系,即在组建组织之前必须有一个缔约方。

并且,所有业务对象都从一个不应该在数据库中结束的BusinessObject基类继承,即仅属性继承。

我想使用“EntityTypeConfiguration”类使用Code First / Fluent API方法,即我有以下用于配置的类:

  1. 用于设置配置的上下文类。
  2. 每个业务对象的各个配置类,即PersonConfiguration,OrganizationConfiguration等
  3. 有谁可以帮助我如何为这种情况定义Fluent配置?或者给我一个例子?

    谢谢! (以下代码)

    PSEUDO CODE ............................................. ......................

    // Business Obsject Base Class
    // Should not be implemented as a database table!!!!
    // No Primary Key Needed Here
    public abstract class BusinessObject  
    {  
       private List<BusinessRule> _businessRules = new List<BusinessRule>();  
       public List<BusinessRule> BusinessRules  
       {
         get { return _businessRules; }
       }
    
       private List<string> _validationErrors = new List<string>();
       public List<string> ValidationErrors
       {
         get { return _validationErrors; }
       }
    
       public DateTime CreatedDate { get; set; }
       public Person CreatedBy { get; set; }
       public ModifiedDate { get; set; }
       public Byte[] RowVersion { get; set; }
    }
    
    // Party Pattern Base Class inherits BusinessObject base class
    // Shared Basis for People and Organizations
    public abstract class Party : BusinessObject
    {
       public virtual int PartyId { get; set; }
       public abstract string DisplayName { get; internal set; }
    }
    
    // Is a Party, Implements both BusinessObject base and Party base
    public class Person : Party
    {
       // Both a Primary and a Foreign Key
       // Should be a one-to-one relationship with Party
       //would like this to be "PersonId" not "PartyId" but it's OK if it is not
       public int PersonId { get; set; }  
    
       pubilc virtual string FirstName { get; set; }
       public virtual string LastName { get; set; }
    
       public override string DisplayName
       {
         get {  return LastName + ", " + FirstName;  }
    
         internal set { }
       }
    }
    
    // Is a Party, Implements both BusinessObject base and Party base
    public class Organization : Party
    {
      // Both a Primary and a Foreign Key
      // Should be a one-to-one relationship with Party
      //would like this to be "PersonId" not "PartyId" but it's OK if it is not
      public int OrganizationId { get; set; }
    
      pubilc virtual string Name { get; set; }
    
      public override string DisplayName
      {
        get {  return Name;  }
    
        internal set { }
      }
    }
    

1 个答案:

答案 0 :(得分:1)

非常棘手的问题 我认为您正在寻找的是实现TPT(Table Per Type)继承。我怀疑EF不会满足你的一些要求。有关更多信息,请访问以下链接:
Entity Framework + Multilevel Inheritance + EF Code first http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx