开发者

Fluent Nhibernate : Self referential many to many

开发者 https://www.devze.com 2023-01-27 06:25 出处:网络
With this class and mapping: Public class Something { public int Id; public IList<Something> Similarthings { get; set; }

With this class and mapping:

Public class Something
{
    public int Id;
    public IList<Something> Similarthings { get; set; }
}

public class SomtehingMapping
    {
        public SomtehingMapping()
        {
            Map(x => x.Id);
            HasManyToMany(x => x.Similarthings )
               .Table("S开发者_StackOverflow中文版omethingsToSimilarthings")
               .ParentKeyColumn("SomethingA_Id")
               .ChildKeyColumn("SomethingB_Id")
               .Cascade.All();
         }
}

You end up with this:

Table SomethingsToSimilarthings
-------------------------------
SomethingA_Id    SomethingB_Id
111              222
222              111

Is there any way of defining this mapping so that the bidirectional relationship is expressed using just one database row?


Have you tried setting up the inverse mapping attribute to true like that?

public class SomtehingMapping
    {
        public SomtehingMapping()
        {
            Map(x => x.Id);
            HasManyToMany(x => x.Similarthings )
               .Inverse()
               .Table("SomethingsToSimilarthings")
               .ParentKeyColumn("SomethingA_Id")
               .ChildKeyColumn("SomethingB_Id")
               .Cascade.All();
         }
}

An alternative would be to explicitly define the other side of the association and mark that as inverse="true"

0

精彩评论

暂无评论...
验证码 换一张
取 消