NHibernate returning duplicate rows -
nhibernate appears returning contents of first row multiple times. many times there actual, distinct rows in database. example, if 1 person has 3 campus affiliations this:
baker college - teacher
bryant elementary - teacher
ohio state university - student
nhibernate return this:
baker college - teacher
baker college - teacher
baker college - teacher
i'm using fluentnhibernate. here snippets of entity , mapping files:
public class person { public virtual string sysid { get; set; } public virtual string fullname { get; set; } public virtual icollection<campus> campuses { get; set; } } public class campus { public virtual string sysid { get; set; } public virtual string name { get; set; } public virtual string affiliation { get; set; } } public class personmapping { table("person"); id(x => x.sysid); map(x => x.fullname).column("full_name"); hasmany(x => x.campuses).keycolumn("sysid"); } public class campusmapping { table("campus"); id(x => x.sysid); map(x => x.name); map(x => x.affiliation); }
i'm iterating through campuses in view (mvc 3) this:
@foreach(var campus in model.campuses) { @campus.name @campus.affiliation }
i tried adding entity make sure wasn't silly mistake mvc or razor , had same result:
public virtual string campusestostring { { string s = ""; (int = 0; < campuses.count; i++) { s = s + campuses.elementat(i).name + " "; } return s; } }
finally, checked sql being output, , it's correct, , it's returning of rows uniquely...
your mapping looks bit weird.
first set relationship: person 1 -> * campus
but in mapping of campus make sysid primary key, foreign key person? think confuses nhibernate..
what think happens nhibernate sees same sysid key multiple times , since resolve same object same primary key preserve object indentity return same campus object multiple times though other columns have different data.
you might want use many-to-many mapping otherwise each campus able have 1 person seems wrong.
Comments
Post a Comment