我无法让 NHibernate在这种一对一关系中映射表列AccountCode
的列(每个都有一个,每个都有一个)。Beneficiary
AccountCode
Account
Beneficiary
Beneficiary
Account
课程:
public class Account
{
...
public virtual string Name { get; protected set; }
public virtual string Code { get; protected set; }
}
public class Beneficiary
{
...
public virtual int Id { get; set; }
public virtual string Name { get; protected set; }
public virtual Account Account { get; protected set; }
public virtual BeneficiaryGroup Group { get; protected set; }
}
SQL:
CREATE VIEW dbo.Account AS
SELECT DISTINCT RTRIM(LTRIM(ACCNT_NAME)) AS Name,
RTRIM(LTRIM(ACCNT_CODE)) AS Code
FROM myremoteserver.schema.tablename
WHERE ACCNT_TYPE NOT IN ('B', 'P')
CREATE TABLE dbo.Beneficiary
(
Id INT IDENTITY(1,1) NOT NULL,
BeneficiaryGroupId INT NOT NULL CONSTRAINT FK_Beneficiaries_BeneficiaryGroup FOREIGN KEY REFERENCES dbo.BeneficiaryGroup (Id),
Name VARCHAR(100) NOT NULL,
AccountCode VARCHAR(100) NOT NULL,
CONSTRAINT PK_Beneficiary PRIMARY KEY (Id)
)
当尝试使用HasMany
和不同的变体时,NHibernate 会尝试加入该Beneficiary.Id
列。
Map
我尝试了,的不同变体References
(Join
它告诉我连接已经存在)和HasMany
(它失败了,因为关系确实是一对一的)。
如何让 NHibernate 将这两个类正确映射到它们的列?
在尝试不同的流利映射时,在 myIAutoMappingOverride<Beneficiary>
中,会发生以下情况:
mapping.HasOne(b => b.Account);
mapping.HasOne(b => b.Account).PropertyRef(sa => sa.Code);
mapping.HasOne(b => b.Account).PropertyRef(sa => sa.Code).ForeignKey("none");
生成的 SQL 使用Beneficiary.Id
字段而不是Beneficiary.AccountCode
. (在你问之前,我使用“none”,因为Account
它是一个视图,它不能有一个键)。
mapping.Join("AccountCode", x => x.References(y => y.Account));
mapping.Join("Account", b => b.KeyColumn("AccountCode"));
结果在Tried to add join to table 'Account' when already added.
.
和:
mapping.Map(b => b.Account, "AccountCode");
mapping.Map(b => b.Account).ReadOnly().Column("AccountCode");
导致:
无法确定类型:.Account,版本=1.0.0.0,Culture=neutral,PublicKeyToken=null,列:NHibernate.Mapping.Column(AccountCode)
mapping.References(b => b.Account).Column("Code");
结果在Invalid column name 'Code'.
.
和:
mapping.References(b => b.Account).Column("AccountCode");
mapping.References(b => b.Account).Column("AccountCode").Access.Property();
覆盖我的所有IReferenceConvention
覆盖(将某些类映射为具有<class name>Code
键列)。
尝试时HasMany
:
mapping.HasMany<Account>(b => b.Account).KeyColumn("AccountCode");
自定义类型未实现 UserCollectionType: .Account