gpt4 book ai didi

c# - Nhibernate:连接表并从其他表中获取单列

转载 作者:行者123 更新时间:2023-11-30 21:23:13 24 4
gpt4 key购买 nike

我有以下表格:

create table Users(
Id uniqueidentifier primary key,
InfoId uniqueidentifier not null unique,
Password nvarchar(255) not null
)

Create table UserInfo(
Id uniqueidentifier primary key,
Company nvarchar(255) not null,
ContactPerson nvarchar(255) not null
)

InfoId是引用 UserInfo(Id) 的外键.

我想将其映射到以下类:

public class UserCredentials
{
public virtual Guid Id { get; set; }
public virtual string UserName { get; set; }
public virtual string PasswordHash { get; set; }

protected UserCredentials() { }
}

我想要以下映射:

Id  --> Users(Id)
UserName --> UserInfo(Company)
PasswordHash --> Users(Password)

我尝试了以下映射:

<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:nhibernate-mapping-2.2">
<class name="UserCredentials" table="Users">
<id name="Id" type="Guid">
<generator class="guid.comb" />
</id>

<property name="PasswordHash" not-null="true" column="Password"/>
<join table="UserInfo">
<key column="Id" foreign-key="InfoId"/>
<property name="UserName" column="Company" not-null="true" />
</join>
</class>
</hibernate-mapping>

但似乎<key>元素指定不正确(foreign-key 属性)不符合我的要求。如果我遗漏了 foreign-key属性,它会尝试加入 Id两个表的列,这是不正确的。

我不想包含 InfoId我的属性(property)UserCredentials类,如果可以避免的话。

任何人都可以帮助我实现所需的映射吗?

最佳答案

当您想加入一个关系或另一个表时,就像您在这里所做的那样,不是基于主键,而是基于其他一些列,您需要使用 property-ref 属性。

像这样:

<property name="InfoId" not-null="true" column="InfoId"/>
<property name="PasswordHash" not-null="true" column="Password"/>

<join table="UserInfo">
<key column="Id" property-ref="InfoId"/>
<property name="UserName" column="Company" not-null="true" />
</join>

这意味着您还需要将 InfoId 添加到您的 UserCredentials 类中,目前无法通过使用列名来映射关系或连接,您必须指定一个属性(NH 将使用该属性的列指)

附带说明一下,外键属性有点误导,它只指定了当您让 nhibernate 创建数据库模式时将生成的数据库外键的名称。

关于c# - Nhibernate:连接表并从其他表中获取单列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1896645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com