Note that there are some explanatory texts on larger screens.

plurals
  1. POnHibernate ManyToManyToMany with Fluent
    text
    copied!<p>I have a structure here I have a ManyToManyToMany relationship.</p> <p>This is my (truncated) fluent mappings.</p> <pre><code>public AgencyMap() { Id(x =&gt; x.AgencyId, "AgencyId"); Map(x =&gt; x.AgencyCode, "AgencyCode"); HasManyToMany&lt;Personnel&gt;(x =&gt; x.Personnel) .WithTableName("AgencyPersonnel") .WithParentKeyColumn("AgencyId") .WithChildKeyColumn("PersonnelId").Cascade.All().LazyLoad(); } public PersonnelMap() { Id(x =&gt; x.PersonnelId, "PersonnelId"); HasManyToMany&lt;Discipline&gt;(x =&gt; x.Disciplines) .WithTableName("AgencyPersonnelDiscipline") .WithParentKeyColumn("AgencyPersonnelId") .WithChildKeyColumn("DisciplineId") .Cascade.SaveUpdate().LazyLoad(); } public DisciplineMap() { SchemaIs("dbo"); Id(x =&gt; x.DisciplineId, "DisciplineId"); Map(x =&gt; x.DisciplineCode, "DisciplineCode"); Map(x =&gt; x.Description, "Description"); } </code></pre> <p>If I then run code like this.</p> <pre><code>Agency agency = m_AgencyRepository.Get(10); var personnel = new Personnel() { ... }; personnel.Disciplines.Add(new Discipline() { ... }); agency.Personnel.Add(personnel); m_AgencyRepository.Save(agency); </code></pre> <p>When I run this code I get this error.</p> <pre><code>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. </code></pre> <p>Enitities look like.</p> <pre><code>public class Agency { public virtual int AgencyId {get; set;} public virtual IList&lt;Personnel&gt; Personnel {get; set;} } public class Personnel { public virtual int PersonnelId {get; set;} public virtual IList&lt;Agency&gt; Agencies {get; set;} public virtual IList&lt;Dependency&gt; Dependencies {get; set;} } public class Dependency { public virtual int DependencyId {get; set;} public virtual string Name {get; set;} } </code></pre> <p>If I add an inverse to the ManyToMany in personnel then the Personnel is saved but not the disciplines (no exception is raised).</p> <p>How should this mapping be done?</p> <p>Edit: </p> <p>I have got some more info. If I disable the constraint AgencyPersonnel_AgencyPersonnelDiscipline_FK1 then it all is inserted ok. This makes me think it is the order nHibernate is inserting that is the problem. What I would expect it to do in order is.</p> <ol> <li>INSERT INTO Personnel</li> <li>Get last key (PersonnelId)</li> <li>INSERT INTO AgencyPersonnelDiscipline with AgencyId and PersonnelId.</li> </ol> <p>This would not violate any constraints.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload