Reputation: 323
I am trying to create entity with two FK from tables Group and Person.
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; }
}
Many-to-many mapping:
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();
Problem is that both constrains in table gp are referencing on table person. Show create table command:
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`)
Want to:
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`)
I tried to delete Inverse() in group mapping and use it in person table but I got following exception: "The relationship has Inverse specified on both sides". Does anyone knows why are constraints referencing one table?
Upvotes: 1
Views: 1838
Reputation: 323
found solution:
HasManyToMany(x => x.ppl)
.Table("gp")
.ChildKeyColumns.Add("p_id");
HasManyToMany(x => x.groups)
.Table("gp")
.ChildKeyColumns.Add("g_id")
Upvotes: 0
Reputation: 27852
I have this:
I do not have "Inverse".
You have (singular)
.ParentKeyColumn("p_id")
.ChildKeyColumn("g_id")
I have adding to the collection, maybe something to try.
.ParentKeyColumns.Add("p_id")
.ChildKeyColumns.Add("g_id")
(and vice versa)
Here are my 2 mappings (unaltered from my world, where Employee(s) have ParkingArea(s) and vice versa)............
/* 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()
;
Upvotes: 1