問題描述
在 NHibernate 上的多對多關係上添加自定義列 (Add custom columns on many to many relationship on NHibernate)
我有一個電影實體和一個演員實體,這兩個實體具有多對多關係,所以我將其映射為 ManyToMany(x=>x.Movies)
和 ManyToMany(x=>x.Actors)
但我想讓演員在電影中扮演的角色,它應該作為新列保留在 MoviesActorsPivot
上
但是我怎樣才能使用 Fluent Nhibernate 映射以一種我可以像 nhibernate 一樣簡單地獲取和保存數據的方式來做到這一點?
不手動創建數據透視表,並在兩側製作 HasMany(x => x.MoviesActorsPivot)
並由我自己管理關聯。
編輯:
或者如果我映射它創建 HasMany(x => x.
參考解法
方法 1:
The answer is:
NHibernate native many‑to‑many
mapping does not support any additional setting on the pairing table
But, it could be replaced with a pairing object being first level citizen
public class MovieActor
{
public virtual Movie Movie { get; set; }
public virtual Actor Actor { get; set; }
... // more properties here
public virtual int Rating { get; set; }
}
public class Actor
{
public virtual IList<MovieActor> Movies { get; set; }
}
public class Movie
{
public virtual IList<MovieActor> Actors { get; set; }
}
That would be standard HasMany
and References
mapping. And the queriyng later will be more easier
Also check these:
- Nhibernate: How to represent Many‑To‑Many relationships with One‑to‑Many relationships?
- nhibernate many to many with multiple table
- many‑to‑many with extra columns nhibernate
(by Diego Barreto、Radim Köhler)
參考文件