![](/img/trans.png)
[英]How to map a reference of a CompositeId using Fluent NHibernate
[英]Confusion using CompositeId / Composite Keys in Fluent NHibernate
請有人可以解釋NHibernate中的復合ID如何工作嗎?
我想我缺少了一些東西。 這就是我在做什么。
我有一個TeamMember類,稱為TeamMemberUnified,因為我試圖將兩個不同的數據庫與視圖鏈接起來以創建一個統一的數據庫。 每個TeamMember實例都與一個人在建築物中扮演的角色有關。
public class TeamMemberUnified: DataItem
{
public virtual int Id { get; set; }
// person / contact details
public virtual byte[] ContactId { get; set; }
public virtual Contact Contact { get; set; }
/// property id
public virtual string PropIdUnified { get; set; }
public virtual string UnifiedDbCode { get; set; }
public virtual int RoleId { get; set; }
// role the person plays at this property
public virtual TeamRoleUnified TeamRole { get; set; }
}
一個人在建築物中扮演的角色由TeamRole類表示。 幾乎所有靜態數據/查找類
public class TeamRoleUnified: DataItem
{
public virtual int RoleId { get; set; }
public virtual string Title{ get; set; }
public virtual string UnifiedDbCode { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
var that = (TeamRoleUnified)obj;
return this.RoleId == that.RoleId &&
this.UnifiedDbCode == that.UnifiedDbCode;
}
public override int GetHashCode()
{
return RoleId.GetHashCode() ^
UnifiedDbCode.GetHashCode();
}
}
這是TeamMember的地圖
public class TeamMemberUnifiedMap : ClassMap<TeamMemberUnified>
{
public TeamMemberUnifiedMap()
{
Id(x => x.Id, "Id");
Map(x => x.ContactId);
Map(x => x.PropIdUnified);
Map(x => x.UnifiedDbCode);
// team member has one role at this building
References(t => t.TeamRole)
.Columns(new string[] {"RoleId", "UnifiedDbCode"});
Table("dbo.TeamMembers");
}
}
而TeamRole與此類映射。 RoleId是一個唯一的ID,但是現在我已經在一個視圖中“合並”了兩個數據庫中的兩個數據庫/引用表,因此RoleId僅在每個Db的行中是唯一的。 Db由UnifiedDbCode限定。
public class TeamRoleUnifiedMap : ClassMap<TeamRoleUnified>
{
public TeamRoleUnifiedMap()
{
CompositeId()
.KeyProperty(x => x.RoleId)
.KeyProperty(x => x.UnifiedDbCode);
//References(x => x.)
Map(x => x.RoleId);
Map(x => x.Title);
Map(x => x.UnifiedDbCode);
Table("dbo.TeamRoles");
}
}
我的理解是,我需要在TeamRole上定義一個復合ID,以定義如何唯一標識行。
但是,當我嘗試運行代碼時,出現此錯誤:
{“外鍵(FK8FB93FCE3B0D5D3E:dbo.TeamMembers [RoleId]))必須具有與引用的主鍵相同的列數(dbo.TeamRoles [RoleId,UnifiedDbCode])”}}
例如,復合ID是具有多個部分的主界面。 假設我們有一張桌子:
|ID|StudentID|Name|Class|
-------------------------
|1 |10023 |Jon |FR |
|1 |10024 |Bob |FR |
|2 |10023 |Jon |SO |
|3 |12234 |Joe |FR |
在這種情況下,假設您只想選擇Jon,但是對於某些共振點,JOn和Bob的ID為1,這並不能唯一地標識Jon。 在那里,我們必須創建一個復合鍵。 因此,我們需要使用復合主鍵(即(ID,StudentID))來代替主鍵作為Just ID,因為如果僅使用學生ID,則會看到兩個Jons,但是如果使用( ID,StudentID),我們將獲得唯一的標識符,只有一行ID為1的學生和學生ID為10023的學生。 希望這可以幫助。 因此,最后您可能有兩個或多個帶有重復值的色散,但這些列在一起可能未必具有唯一值。 希望這可以幫助。 抱歉,這張桌子看起來有些古怪。
供參考,現在正在工作。
這是主鍵上的TeamRole映射(一對多的一側):
CompositeId()
.KeyProperty(x => x.RoleId)
.KeyProperty(x => x.UnifiedDbCode);
這是外鍵映射(一對多的多面):
// team member has one role at this building
References(t => t.TeamRole)
.ForeignKey("TeamRoleFK")
.Columns(new string[] {"RoleId", "UnifiedDbCode"})
.Not.Update()
.Not.Insert();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.