NHibernate 加入错误字段

发布于 2024-11-05 18:50:45 字数 1324 浏览 1 评论 0 原文

我对 Nhibernate 非常陌生,我知道我的问题是什么,但不确定如何解决它。

问题:用户通过 EmployeeID 而不是 UserID 加入到员工。这导致了问题,因为它返回了错误的详细信息。

Employee.hbm.xml
<many-to-one name="User" unique="true" column="UserID" />

User.hbm.xml
<one-to-one name="Employee" foreign-key="EmployeeID" class="Employee" lazy="false" />

Employee.cs
public virtual int EmployeeID { get; set; }
public virtual User User { get; set; } - This is UserID within the actual database
public virtual string EmailAddress { get; set; }

User.cs
public virtual int UserID { get; set; }
public virtual string Username { get; set; }
public virtual string Title { get; set; }
public virtual string Forename { get; set; }
public virtual string Surname { get; set; }
public virtual Employee Employee { get; set; }

上面是其他人编码的内容,我正在尝试修复它。我尝试更改

<one-to-one name="Employee" foreign-key="UserID" class="Employee" lazy="false" />

但这仍然会导致问题。

我错过了什么吗?提前感谢您的帮助:-)

Clare

更新

我希望通过使其一对一地加入到 UserID 上,但它仍然加入到 EmployeeID 上。有人还有其他想法吗?再次感谢 :-)

Employee.hbm.xml
<one-to-one name="User" foreign-key="UserID" class="User" lazy="false" />

User.hbm.xml
<one-to-one name="Employee" foreign-key="UserID" class="Employee" lazy="false" />

I am very new to Nhibernate, I know what my issue is however unsure how to fix it.

Issue: The user is joined to a employee via the EmployeeID rather than the UserID. Which is causing issues as it is bringing back incorrect details.

Employee.hbm.xml
<many-to-one name="User" unique="true" column="UserID" />

User.hbm.xml
<one-to-one name="Employee" foreign-key="EmployeeID" class="Employee" lazy="false" />

Employee.cs
public virtual int EmployeeID { get; set; }
public virtual User User { get; set; } - This is UserID within the actual database
public virtual string EmailAddress { get; set; }

User.cs
public virtual int UserID { get; set; }
public virtual string Username { get; set; }
public virtual string Title { get; set; }
public virtual string Forename { get; set; }
public virtual string Surname { get; set; }
public virtual Employee Employee { get; set; }

Above is what someone else has coded and I'm trying to fix it. I tried changing

<one-to-one name="Employee" foreign-key="UserID" class="Employee" lazy="false" />

however this still causes issues.

Am I missing something? Thanks in advance for any help :-)

Clare

UPDATE

I was hoping by making it one-to-one that it would join on the UserID, however it is still joining on the EmployeeID. Anyone have any other ideas? Thanks again :-)

Employee.hbm.xml
<one-to-one name="User" foreign-key="UserID" class="User" lazy="false" />

User.hbm.xml
<one-to-one name="Employee" foreign-key="UserID" class="Employee" lazy="false" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心病无药医 2024-11-12 18:50:45

您不应在 一对一 映射中指定任何列名称,因为该信息是多对一映射到关系的另一端。

为了解决您的问题,您应该从 User 实体上的 one-to-one 映射中删除 foreign-key 属性:

<one-to-one name="Employee" class="Employee" lazy="false" />

NHibernate 将使用在 Employee 实体上定义的多对一映射中的信息来构建 SQL 语句。

注意,如果 Employee 总是有一个与其关联的 User(即 Employee.User 属性永远不能null),那么您应该通过添加 constrained 属性在一对一映射中声明这一事实:

<one-to-one name="Employee" class="Employee" constrained="true" lazy="false" />

相关资源:< /强>

  • <一href="http://ayende.com/Blog/archive/2009/04/19/nhibernate-mapping-ltone-to-onegt.aspx" rel="nofollow">NHibernate 映射 - <一对一 / >

You shouldn't specify any column name in a one-to-one mapping, because that piece of information is part of the many-to-one mapping on the other side of the relationship.

In order to solve your problem you should remove the foreign-key attribute from the one-to-one mapping on the User entity:

<one-to-one name="Employee" class="Employee" lazy="false" />

NHibernate will use the information in the many-to-one mapping defined on the Employee entity to build the SQL statements.

Note that if an Employee always has a User associated to it (i.e. the Employee.User property can never be null) then you should state this fact in the one-to-onemapping by adding the constrained attribute:

<one-to-one name="Employee" class="Employee" constrained="true" lazy="false" />

Related resources:

咆哮 2024-11-12 18:50:45

根据您的实体,用户和员工之间不存在一对一的关系。请参阅此解释:

http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/

我认为你需要将你的一对一指定为改为多对一。

编辑:
我想我可能误解了你的情况。
您可能想在此处使用 property-ref。因此,您的映射将如下所示:

<one-to-one name="Employee" class="Employee" property-ref="User" lazy="false" />

请查看以下链接以获取更多详细信息:Nhibernate 文档

这里也有一个类似的帖子:一对一并指定列

According to your entities there is not a one to one relationship between user and employee. Please see this explanation:

http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/

I think you need to specifiy your one to one as a many-to-one instead.

Edit:
I think I may have misunderstood your scenario.
You may want to use property-ref here. So you're mapping would look like this:

<one-to-one name="Employee" class="Employee" property-ref="User" lazy="false" />

Please check out the following link for more detail: Nhibernate Documentation

Also there is a similar post on SO here: one-to-one and specifying column

注定孤独终老 2024-11-12 18:50:45

我将在这里使用多对一关系,因为我知道您可能想要合并用户帐户,而一对一关系可能会导致将员工记录移动到合并用户时出现问题。

如果您将其视为您的用户可以拥有多个员工记录,那么就可以避免此问题。

您是否尝试过类似以下内容:

Employee.hbm.xml

用户.hbm.xml

http://ayende.com/blog/3938/nhibernate-mapping-many一对一

I would use a many-to-one relationship here because I am aware you may want to merge User accounts and one-to-one may cause problems with moving an employee record to the merged user.

If you treat it like your User can have multiple Employee records then this avoids this problem.

Have you tried something like the following:

Employee.hbm.xml

User.hbm.xml

http://ayende.com/blog/3938/nhibernate-mapping-many-to-one

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文