No one seems much interested in this question, but 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.
1. INSERT INTO Personnel 2. Get last key (PersonnelId) 3. Insert into AgencyPersonnelDiscipline with AgencyId and PersonnelId. This would not violate any constraints. On Thu, May 14, 2009 at 11:25 PM, Craig van Nieuwkerk <[email protected]> wrote: > I have a structure where I have a ManyToManyToMany relationship. > > This is my (truncated) fluent 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 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 get 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. > > If I add an inverse to the ManyToMany in personnel then the Personnel > is saved but not the disciplines (no exception is raised). > > How should this mapping be done? > > > Craig. > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
