NHibernate ManyToManyToMany with Free

I have a structure here, I have a ManyToManyToMany relationship.

These are my (truncated) flowing mappings.

public AgencyMap() 
{
    Id(x => x.AgencyId, "AgencyId");            
    Map(x => x.AgencyCode, "AgencyCode");
    HasManyToMany<Personnel>(x => x.Personnel)
    .WithTableName("AgencyPersonnel")
    .WithParentKeyColumn("AgencyId")
        .WithChildKeyColumn("PersonnelId").Cascade.All().LazyLoad();
}

public PersonnelMap() 
{
    Id(x => x.PersonnelId, "PersonnelId");    
    HasManyToMany<Discipline>(x => x.Disciplines)
        .WithTableName("AgencyPersonnelDiscipline")
        .WithParentKeyColumn("AgencyPersonnelId")
        .WithChildKeyColumn("DisciplineId")
        .Cascade.SaveUpdate().LazyLoad();
}           

public DisciplineMap() 
{
    SchemaIs("dbo");            
    Id(x => x.DisciplineId, "DisciplineId");            
    Map(x => x.DisciplineCode, "DisciplineCode");           
    Map(x => x.Description, "Description");         
}   

      

If I then run a code like this.

Agency agency = m_AgencyRepository.Get(10);
var personnel = new Personnel() { ... };
personnel.Disciplines.Add(new Discipline() { ... });
agency.Personnel.Add(personnel);
m_AgencyRepository.Save(agency);

      

When I run this code, I am getting this error.

could not insert collection: [PPSS.Model.Personnel.Disciplines#22][SQL: SQL not available]
The INSERT statement conflicted with the FOREIGN KEY constraint "AgencyPersonnel_AgencyPersonnelDiscipline_FK1". The conflict occurred in database "PPSS2", table "dbo.AgencyPersonnel", column 'AgencyPersonnelId'.\r\nThe statement has been terminated.

      

The ghosts are looking.

public class Agency
{
  public virtual int AgencyId {get; set;}
  public virtual IList<Personnel> Personnel {get; set;}
}

public class Personnel
{
  public virtual int PersonnelId {get; set;}
  public virtual IList<Agency> Agencies {get; set;}
  public virtual IList<Dependency> Dependencies {get; set;}
}

public class Dependency
{
  public virtual int DependencyId {get; set;}
  public virtual string Name {get; set;}
}

      

If I add the inverse of ManyToMany to Personnel, then Personnel will be retained, but no Disciplines (no exception will be thrown).

How should this mapping be done?

Edit:

I have more information. If I disable the AgencyPersonnel_AgencyPersonnelDiscipline_FK1 constraint, then everything is pasted ok. This makes me think that this is the question that nHibernate introduces. I would expect this to be done in order.

  • INSERT INTO Personnel
  • Get the last key (PersonnelId)
  • INSERT INTO AgencyPersonnelDiscipline with AgencyId and PersonnelId.

This will not violate any restrictions.

+1


a source to share


2 answers


Since this is a truncated display, I'm going to take a picture in the dark :). Usually this kind of thing happens to me when I have two objects, X and Y, and I match them from both directions, something like:

X has the property Y



Y has a list X

And none of them have an Inverse () attribute. Check that you are matching something in both directions, but not setting Inverse () on them.

0


a source


Given that the maps you have are correct for the types you specify, you have not mapped back from Personnel to Agency or from Discipline to Personnel.



0


a source







All Articles