Basic Fluent NHibernate relationship issue -
Basic Fluent NHibernate relationship issue -
the project working on @ moment using entity framework, there issues have come across , hence researching using nhibernate believe sort out bulk of issues have.
anyway, have been replicating simple part of system, have ran assume simple problem one-to-many relationship giving unusual results.
here entities:
public class task : base.domain { private ilist<taskproperty> _taskproperties = new bindinglist<taskproperty>(); private string _name = string.empty; private string _description = string.empty; public virtual ilist<taskproperty> taskproperties { { homecoming _taskproperties; } set { if (_taskproperties == value) return; _taskproperties = value; onnotifiypropertychanged("taskproperties"); } } public virtual string name { { homecoming _name; } set { if (_name == value) return; _name = value; base.onnotifiypropertychanged("name"); } } public virtual string description { { homecoming _description; } set { if (_description == value) return; _description = value; base.onnotifiypropertychanged("description"); } } public task() : base() { } } public class taskproperty : base.domain { private task _task = null; private string _name = string.empty; private string _description = string.empty; private int _propertytype = 0; //public virtual int taskid { get; set; } public virtual task task { { homecoming _task; } set { if (_task == value) return; _task = value; onnotifiypropertychanged("task"); } } public virtual string name { { homecoming _name; } set { if (_name == value) return; _name = value; onnotifiypropertychanged("name"); } } public virtual string description { { homecoming _description; } set { if (_description == value) return; _description = value; onnotifiypropertychanged("description"); } } public virtual int propertytype { { homecoming _propertytype; } set { if (_propertytype == value) return; _propertytype = value; onnotifiypropertychanged("propertytype"); } } public taskproperty() : base() { } }
here nhibernate mappings:
public class taskmapping : classmap<task> { public taskmapping() { id(x => x.id).column("rettaskid"); map(x => x.name); map(x => x.description); map(x => x.version); hasmany(x => x.taskproperties).keycolumn("rettaskpropertyid"); table("rettask"); } } public class taskpropertymapping : classmap<taskproperty> { public taskpropertymapping() { id(x => x.id).column("rettaskpropertyid"); map(x => x.name); map(x => x.description); map(x => x.propertytype); references(x => x.task).column("rettaskid"); table("rettaskproperty"); } }
note: domain class these entities inherit holds id (int id).
the problem facing when task database id of 27 example, taskproperty id of 27 well, not expected 4 taskproperties related task via foreign key.
this worked fine in entity framework , know simple situation orm, assume have set mappings incorrectly, examples have found, don't seem doing wrong!
any answers/suggestions welcome. thanks.
you there. column mapping hasmany , references must same:
public taskmapping() { ... hasmany(x => x.taskproperties).keycolumn("rettaskid"); // utilize // hasmany(x => x.taskproperties).keycolumn("rettaskpropertyid"); // instead of } public taskpropertymapping() { ... references(x => x.task).column("rettaskid"); }
the collection item has have reference column owner. column used both directions, because that's how reference in db managed...
nhibernate fluent-nhibernate
Comments
Post a Comment