簡體   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