UpTheCreek
UpTheCreek

Reputation: 32381

Fluent Nhibernate : Self referential many to 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();
         }
}

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?

Upvotes: 3

Views: 396

Answers (1)

amavroudakis
amavroudakis

Reputation: 2362

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"

Upvotes: 1

Related Questions