NHibernate Save Is Trying to Clear Child KeyColumn Id On Update -
i trying create parent object has multiple children in 1 many relationship. not referencing parent object on child object, instead mapping keycolumn field.
when try save object first time, works expected without issues (cascading id's , children). when try object database, update properties on , re-save again, fails. actual error message getting "could not delete collection".
the error above due fact trying set "parentid" field on child objects null (which violates fk constraint have in db). if remove constraint db, end result want; however, not want perform update (setting parent id null) @ , i'm not sure why is. can tell in sql code generating , sending db, else appears correct , work if wasnt last update statement.
obviously, must have wrong mapping cannot figure out what. tried adding not.keyupdate() made not generate key @ all. have ideas doing wrong??
thanks in advance, appreciate it!!!
please see below mapping:
public class parent { public parent() { children = new list<child>(); } public virtual guid id { get; set; } public virtual ilist<child> children { get; set; } } public class child { public virtual guid id { get; set; } public virtual guid parentid { get; set; } } public class parentmap : classmap<parent> { public parentmap() { table("parent"); id(x => x.id); hasmany(x => x.children).keycolumn("parentid").cascade.saveupdate().not.lazyload(); } } public class childmap : classmap<child> { public childmap() { table("child"); id(x => x.id); map(x => x.parentid); } }
this caused fact, collection of children not marked inverse="true"
.
what can is: i. remove constraint db. nhiberante must (without inverse setting) 2 steps. firstly update record break relation, secondly (due cascades) alse delete item
ii. change mapping, , entities. this:
a child
must have reference parent
:
public class child { public virtual guid id { get; set; } public virtual guid parentid { get; set; } public virtual parent parent { get; set; } }
mapping this:
public class parentmap : classmap<parent> { public parentmap() { table("parent"); id(x => x.id); hasmany(x => x.children) .keycolumn("parentid") .cascade.saveupdate() .not.lazyload() .inverse() // here have inverse setting ; } } public class childmap : classmap<child> { public childmap() { table("child"); id(x => x.id); map(x => x.parentid).not.insert().not.update(); // readonly references(x => x.parent).column("parentid"); // parent writable } }
now, have time set relation in c# code. i.e. if child added parents collection, should set parent reference
parent.children.add(child); child.parent = parent;
nhibernate issue 1 statement, delete child table
Comments
Post a Comment