繁体   English   中英

与继承一对一关系的Entity Framework 4代码优先Fluent API配置

[英]Entity Framework 4 Code First Fluent API Configuration for One-to-One Relationship with Inheritance

我正在尝试首先将EF代码和Fluent Configuration一起用于我的所有业务类,以实现“方”域模式。 配置让我喝酒(不一定不好,但下一站是悬崖)

我想发生的事情类似于下面的代码。 该数据库的要求是我得到下表:

单独的“方”表单独的“人”表单独的“组织”表

人员表与当事人表具有必要的一对一关系,即在拥有人员之前,必须先有一个当事人。

组织表与参与者表之间具有必要的一对一关系,即,在拥有组织之前,必须先有一个参与者。

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

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

  1. 设置配置的上下文类。
  2. 每个业务对象的单独配置类,即PersonConfiguration,OrganizationConfiguration等。

谁能帮我定义这种情况下的Fluent配置吗? 还是给我举个例子?

谢谢! (下面的代码)

伪代码...................................................... ...................

// 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 { }
  }
}

相当棘手的问题。
我认为您正在寻找的是实现TPT(每种类型的表)继承。 我怀疑EF无法满足您的某些要求。 有关更多信息,请访问以下链接:
实体框架+多级继承+ EF代码优先 http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2 -table每次型tpt.aspx

暂无
暂无

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

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