Free Nhibernate: Self-Realizing Many for Many

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("SomethingsToSimilarthings")
               .ParentKeyColumn("SomethingA_Id")
               .ChildKeyColumn("SomethingB_Id")
               .Cascade.All();
         }
}

The result is the following:

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

Is there a way to define this mapping so that the bidirectional relation is expressed using only one database row?

+3
source share
1 answer

Have you tried setting the inverse matching attribute to true ?

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 clearly identify the other side of the association and note that how inverse="true"

+1
source

Source: https://habr.com/ru/post/1777060/


All Articles