![](/img/trans.png)
[英]Fluent NHibernate or NHibernate for complicated database with complicated relations
[英]Fluent NHibernate complicated (?) relations
我在使用Fluent NHibernate創建關系時遇到了麻煩。 通常,我有兩個表,資源和項目:
請注意,資源表中的PK既是id又是語言環境。 這意味着一個項目實際上可以擁有很少的資源(相同的ID,但不同的語言環境)。
因為這不是一對一的簡單關系,所以我很難使用Fluent NHibernate映射這兩個關系。
解決這個問題的正確方法是什么?
非常感謝!
如果這種關系使得給定的Resource
由單個給定的Item
擁有,則可以這樣建模(注意:僅包括重要的部分):
public class Item
{
public virtual int Id { get; protected set; }
public virtual IList<Resource> Resources { get; protected set; }
// Constructor, Equals, GetHashCode, other things ... omitted.
}
public class Resource
{
public virtual Item Owner { get; protected set; }
public virtual int ResourceId { get; protected set; }
public virtual string Locale { get; protected set; }
public virtual string Value { get; protected set; }
// Constructor, Equals, GetHashCode, other things ... omitted.
}
並創建以下類映射:
public class ItemMap : ClassMap<Item>
{
public ItemMap()
{
WithTable("items");
Id(x => x.Id); // add Id generation cfg if needed
HasMany(x => x.Resources)
.Inverse()
.Cascade.All()
}
}
public class ResourceMap : ClassMap<Resource>
{
public ResourceMap()
{
WithTable("resources")
CompositeId()
.KeyProperty(x => x.ResourceId)
.KeyProperty(x => x.Locale);
References(x => x.Owner)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.