[英]Fluent nHibernate many-to-many mapping
我正在嘗試使用表Group和Person中的兩個FK創建實體。
public class Person
{
public virtual int p_id { get; set; }
public virtual string name { get; set; }
public virtual IList<Person> ppl { get; set; }
}
public class Group
{
public virtual int g_id { get; set; }
public virtual string name{ get; set;}
public virtual IList<Group> groups { get; set; }
}
多對多映射:
HasManyToMany(x => x.ppl)
.Table("gp")
.ParentKeyColumn("g_id")
.ChildKeyColumn("p_id");
HasManyToMany(x => x.groups)
.Table("gp")
.ParentKeyColumn("p_id")
.ChildKeyColumn("g_id")
.Inverse();
問題是表gp中的兩個約束都引用表人。 顯示創建表命令:
CREATE TABLE `gp` (
`g_id` int(11) NOT NULL,
`p_id` int(11) NOT NULL,
KEY `p_id` (`p_id`),
KEY `g_id` (`g_id`),
CONSTRAINT `FK4B9E89089BCCDC65` FOREIGN KEY (`g_id`) REFERENCES `person` (`p_id`),
CONSTRAINT `FK4B9E8908E34BCD9E` FOREIGN KEY (`p_id`) REFERENCES `person` (`p_id`)
想要:
CREATE TABLE `gp` (
`g_id` int(11) NOT NULL,
`p_id` int(11) NOT NULL,
KEY `p_id` (`p_id`),
KEY `g_id` (`g_id`),
CONSTRAINT `FK4B9E89089BCCDC65` FOREIGN KEY (`g_id`) REFERENCES `group` (`g_id`),
CONSTRAINT `FK4B9E8908E34BCD9E` FOREIGN KEY (`p_id`) REFERENCES `person` (`p_id`)
我試圖在組映射中刪除Inverse()並在人員表中使用它,但出現以下異常:“關系在兩邊都指定了Inverse”。 有誰知道約束為什么引用一個表?
我有這個:
我沒有“反向”。
你有(單數)
.ParentKeyColumn("p_id") .ChildKeyColumn("g_id")
我已添加到集合中,也許可以嘗試一下。
.ParentKeyColumns.Add("p_id")
.ChildKeyColumns.Add("g_id")
(and vice versa)
這是我的2個映射(與我的世界一樣,那里的員工有ParkingArea,反之亦然)............
/* the below is how to do it without a surogatekey on the link table */
HasManyToMany<ParkingAreaNHEntity>(x => x.MyParkingAreas)
.Table("EmployeeToParkingAreaLink")
.ParentKeyColumns.Add("AbcEmployeeUUID", p => p.UniqueKey("Emp_ParkingArea_Unique").Index("IX_ABC123"))
.ChildKeyColumns.Add("AbcParkingAreaUUID", p => p.UniqueKey("Emp_ParkingArea_Unique"))
.Cascade.None()
;
/* the below is how to do it without a surogatekey on the link table */
HasManyToMany<EmployeeNHEntity>(x => x.MyEmployees)
.Table("EmployeeToParkingAreaLink")
.ParentKeyColumns.Add("AbcParkingAreaUUID", p => p.UniqueKey("Emp_ParkingArea_Unique").Index("IX_ABC123"))
.ChildKeyColumns.Add("AbcEmployeeUUID", p => p.UniqueKey("Emp_ParkingArea_Unique"))
.Cascade.None()
;
找到解決方案:
HasManyToMany(x => x.ppl)
.Table("gp")
.ChildKeyColumns.Add("p_id");
HasManyToMany(x => x.groups)
.Table("gp")
.ChildKeyColumns.Add("g_id")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.