我对 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" />
发布评论
评论(3)
您不应在
一对一
映射中指定任何列名称,因为该信息是多对一映射到关系的另一端。
为了解决您的问题,您应该从
User
实体上的one-to-one
映射中删除foreign-key
属性:NHibernate 将使用在
Employee
实体上定义的多对一
映射中的信息来构建 SQL 语句。注意,如果
Employee
总是有一个与其关联的User
(即Employee.User
属性永远不能null
),那么您应该通过添加constrained
属性在一对一
映射中声明这一事实:相关资源:< /强>
You shouldn't specify any column name in a
one-to-one
mapping, because that piece of information is part of themany-to-one
mapping on the other side of the relationship.In order to solve your problem you should remove the
foreign-key
attribute from theone-to-one
mapping on theUser
entity:NHibernate will use the information in the
many-to-one
mapping defined on theEmployee
entity to build the SQL statements.Note that if an
Employee
always has aUser
associated to it (i.e. theEmployee.User
property can never benull
) then you should state this fact in theone-to-one
mapping by adding theconstrained
attribute:Related resources:
根据您的实体,用户和员工之间不存在一对一的关系。请参阅此解释:http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/
我认为你需要将你的一对一指定为改为多对一。编辑:
我想我可能误解了你的情况。
您可能想在此处使用
property-ref
。因此,您的映射将如下所示:请查看以下链接以获取更多详细信息: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: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
我将在这里使用多对一关系,因为我知道您可能想要合并用户帐户,而一对一关系可能会导致将员工记录移动到合并用户时出现问题。
如果您将其视为您的用户可以拥有多个员工记录,那么就可以避免此问题。
您是否尝试过类似以下内容:
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