Free nHibernate - Matching children to compound keys.

For easy installation parent -> child (CK, CK) like this. I'm having trouble adding a new child and getting the parent link. Therefore, I would add an object in this way.

var parent = new Parent{
  Children = new List<Child>{
   new Child{
     Other = otherReference
   }
  }
};

Or even adding it using a method Add()...

parent.Children.Add (new Child {Other = other});

Link to Parentfails. It just ends up as a null property. I get the following exception.

{"Cannot insert NULL into column" ParentId ", table" mssql_test.Children ", column does not allow zeros. INSERT does not work. \ R \ nApplication is complete." }

I can do it ...

new Child { 
   Parent = parentReference,
   Other = otherReference
}

. , . , , . - ? .

class Parent {
  int Id { get; set; }
  IList<Child> Children { get; set; }
}
class Other {
  int Id { get; set; }
}
class Child {
  Parent Parent { get; set; }
  Other Other { get; set; }
  // other properties
}

Mapping

 ChildMap() {
      CompositeId()
        .KeyReference(x => x.Parent, "ParentId")
        .KeyReference(x => x.Other, "OtherId");
    }

    ParentMap(){
     HasManyToMany(x => x.Children)
                    .AsBag()
                    .ChildKeyColumns.Add(new[] { "ParentId", "OtherId" })
                    .Inverse()
                    .Cascade.All())
                    .Table("[Test]");
}
+3
2

@KeithS, , Child HasManyToMany, HasMany. :

  ChildMap() {
      CompositeId() //This is is good
        .KeyReference(x => x.Parent, "ParentId")
        .KeyReference(x => x.Other, "OtherId");
  }

  ParentMap(){ //This is the fix
        HasMany(c => c.Children)
          .Inverse()
          .Cascade.All()
          .KeyColumn("ParentId") //Not needed if Parent Id prop maps to ParentId col
          .Table("[Test]");
  }
+3

. NHibernate ( FluentNH) , ; , , NH , . "" , " " FK. " " .

, , "--" -- ( HasMany). , . ManyToMany , .

0

All Articles