NHibernate 返回重复行
NHibernate 似乎多次返回第一行的内容。与数据库中实际存在的不同行一样多。例如,如果一个人有 3 个校园隶属关系,如下所示:
贝克学院 - 教师
布莱恩特小学 - 老师
俄亥俄州立大学 - 学生
NHibernate 将像这样返回:
贝克学院 - 教师
贝克学院 - 教师
贝克学院 - 老师
我正在使用 FluentNHibernate。以下是实体和映射文件的一些片段:
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);
}
我在我的视图 (MVC 3) 中迭代校园,如下所示:
@foreach(var campus in Model.Campuses)
{
@campus.Name @campus.Affiliation
}
我还尝试将其添加到实体中,以确保这不是 MVC 或 Razor 的愚蠢错误并得到相同的结果:
public virtual string campusesToString
{
get
{
string s = "";
for (int i = 0; i < Campuses.Count; i++)
{
s = s + Campuses.ElementAt(i).Name + " ";
}
return s;
}
}
最后,我检查了正在输出的 SQL,它是正确的,并且它唯一地返回所有行......
NHibernate appears to be returning the contents of the first row multiple times. As many times as there are actual, distinct rows in the database. For example, if one person has 3 campus affiliations like this:
Baker College - Teacher
Bryant Elementary - Teacher
Ohio State University - Student
NHibernate will return it like this:
Baker College - Teacher
Baker College - Teacher
Baker College - Teacher
I'm using FluentNHibernate. Here are some snippets of the entity and 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 the campuses in my view (MVC 3) like this:
@foreach(var campus in Model.Campuses)
{
@campus.Name @campus.Affiliation
}
I also tried adding this to the entity to make sure it wasn't a silly mistake with MVC or Razor and had the same result:
public virtual string campusesToString
{
get
{
string s = "";
for (int i = 0; i < Campuses.Count; i++)
{
s = s + Campuses.ElementAt(i).Name + " ";
}
return s;
}
}
Finally, I checked the SQL that was being output, and it's correct, and it's returning all of the rows uniquely...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的映射看起来有点奇怪。
首先,您设置此关系:
Person 1 -> * Campus
但是在 Campus 的映射中,您将 SysId 设置为主键,但它也是 Person 的外键?我认为这就是 NHibernate 困惑的地方。
我认为发生的情况是 NHibernate 多次看到相同的 SysId 键,并且由于它将解析相同主键的相同对象以保留对象标识,因此它会多次返回相同的 Campus 对象尽管其他列有不同的数据。
您可能想要使用多对多映射,否则每个校区只能有一个人,这似乎是错误的。
Your mapping looks a bit weird.
First you set up this relationship:
Person 1 -> * Campus
But in your mapping of Campus you make SysId primary key, but it is also the foreign key to Person? I think that is what confuses NHibernate..
What I think happens is that NHibernate sees the same SysId key multiple times and since it will resolve the same object for the same primary key to preserve object indentity it will return the same Campus object multiple times even though the other columns have different data.
You might want to use a many-to-many mapping as otherwise each Campus will only be able to have one person which seems wrong.
好的。我发现了我的问题。我在映射中错误地指示了 Id。
一个人,多个校园。人员 ID 称为 SysID。它也是校园里的外键。但SysID并不是校园的唯一ID。 NHibernate 很困惑,因为它试图使用个人唯一 ID 作为校园 ID。
Ok. I found out my problem. I was indicating the Id in the mapping incorrectly.
There is one person and multiple campuses. The person ID is called SysID. It is also the foreign key on campus. But SysID is not the unique ID for campus. NHibernate was confused because it was trying to use the person unique ID for the campus ID.