一千萬個為什麽

搜索

子類和連接子類的NHibernate映射


在我正在處理的項目中,我有以下實體:分析人員客戶端承包商。每個都繼承自基類 User

public abstract class User {
    public virtual int Id { get; set; }
    public virtual string Username { get; set; }
    public virtual string FullName { get; set; }
}

然後我將其他類繼承自基類:

public class Analyst : User {
   //Empty class. There are no additional properties for an analyst.
}

public class Client : User {
   //Empty class. There are no additional properties for a client.
}

public class Contractor : User {
    public int TotalJobs { get; set; }
    public int JobsInProgress { get; set; }
}

對於上面的類,我有以下表結構:

USER
----
UserId
Username
FullName
UserType (1 = Analyst, 2 = Client, 3 = Contractor)


CONTRACTOR
----------
UserId
TotalJobs
JobsInProgress

Analyst和Client類沒有表。

我想知道如何為Contractor類編寫NHibernate映射文件。對於其他類,我創建了一個用戶映射文件,並將Client和Analyst添加為子類。如何映射Contractor類?

最佳答案

This is a perfect fit for the approach described in 8.1.4. Mixing table per class hierarchy with table per subclass


  
    
    
    
  

轉載註明原文: 子類和連接子類的NHibernate映射

猜你喜歡