2011-12-15 70 views
2

我想實現「黨」域模式使用EF代碼首先與我的所有業務類的Fluent配置。配置讓我喝酒(不是一件壞事,但下一站就是懸崖)實體框架4代碼第一流暢的API配置與繼承的一對一關係

我想要發生的事情就像下面顯示的代碼一樣。數據庫的要求是我最終下表:

一個單獨的「雙方」表 一個獨立的「人」表 一個獨立的「組織」表

百姓餐桌都需要有一個一與締約方表之間的「一對一」關係,即在擁有某人之前,您必須有一方。

組織表與參與方表具有必需的一對一關係,即在組織之前您必須有一方。

而且,所有業務對象都繼承自BusinessObject基類,該基類不應該在數據庫中結束,即僅屬性繼承。

我想用「EntityTypeConfiguration」類使用一個代碼優先/流利API方法即我有配置以下類:

  1. 甲上下文類爲settup向上配置。
  2. 個別配置類爲每個業務對象即PersonConfiguration,OrganizationConfiguration等

任何人都可以請幫我如何定義這個場景流利的配置?或者讓我指出一個例子?

謝謝! (代碼如下)

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

回答

相關問題