简体   繁体   English

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

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

I'm trying to implement the "Party" domain pattern using EF Code first with Fluent Configuration for all my business classes. 我正在尝试首先将EF代码和Fluent Configuration一起用于我的所有业务类,以实现“方”域模式。 The configuration is driving me to drink (not a bad thing necessarily but next stop is the cliff) 配置让我喝酒(不一定不好,但下一站是悬崖)

What I would like to have happen is something like the code shown below. 我想发生的事情类似于下面的代码。 The requirements for the database are that I end up with the following tables: 该数据库的要求是我得到下表:

A separate "Parties" Table A separate "People" Table An separate "Organizations" Table 单独的“方”表单独的“人”表单独的“组织”表

The People Table has a required One-To-One relationship with the Parties Table, ie you must have a Party before you have a Person. 人员表与当事人表具有必要的一对一关系,即在拥有人员之前,必须先有一个当事人。

The Organization Table has a required One-To-One relationship with the Parties Table, ie you must have a Party before you have an Organization. 组织表与参与者表之间具有必要的一对一关系,即,在拥有组织之前,必须先有一个参与者。

And, all business objects inherit from a BusinessObject base class that should Not end up in the database, ie property inheritence only. 并且,所有业务对象都从不应在数据库中结束的BusinessObject基类继承,即仅属性继承。

I want to use a Code First / Fluent API approach using "EntityTypeConfiguration" classes ie I have the following classes for configuration: 我想使用“ EntityTypeConfiguration”类使用Code First / Fluent API方法,即我具有以下用于配置的类:

  1. A Context Class for settup up configuration. 设置配置的上下文类。
  2. Individual configuration classes for each business object ie PersonConfiguration, OrganizationConfiguration, etc. 每个业务对象的单独配置类,即PersonConfiguration,OrganizationConfiguration等。

Can anyone please help me with how to define Fluent Configuration for this scenario? 谁能帮我定义这种情况下的Fluent配置吗? Or point me to an example? 还是给我举个例子?

Thanks! 谢谢! (Code Below) (下面的代码)

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

Pretty tough question. 相当棘手的问题。
I think what you're looking for is implementing TPT (Table Per Type) inheritance. 我认为您正在寻找的是实现TPT(每种类型的表)继承。 I suspect some of your requirements will not be satisfied by EF. 我怀疑EF无法满足您的某些要求。 For more information visit the following links: 有关更多信息,请访问以下链接:
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 实体框架+多级继承+ 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