Ode*_*ded 3 sql-server-2000 view fluent-nhibernate
我无法获得的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; }
}
Run Code Online (Sandbox Code Playgroud)
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)
)
Run Code Online (Sandbox Code Playgroud)
当尝试使用HasMany和不同的变体时,NHibernate尝试加入该Beneficiary.Id列.
我曾尝试不同的变化Map,References,Join(它告诉我,加入已经存在)和HasMany(其失败,由于工作的关系确实是一个对一个).
我怎样才能让NHibernate将这两个类正确映射到它们的列?
在尝试不同的流畅映射时,在我看来IAutoMappingOverride<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");
Run Code Online (Sandbox Code Playgroud)
生成的SQL使用Beneficiary.Id字段而不是Beneficiary.AccountCode.(在你问之前,我使用"无"因为Account是一个视图,它不能有一个键).
mapping.Join("AccountCode", x => x.References(y => y.Account));
mapping.Join("Account", b => b.KeyColumn("AccountCode"));
Run Code Online (Sandbox Code Playgroud)
结果Tried to add join to table 'Account' when already added..
和:
mapping.Map(b => b.Account, "AccountCode");
mapping.Map(b => b.Account).ReadOnly().Column("AccountCode");
Run Code Online (Sandbox Code Playgroud)
造成:
无法确定以下类型:.Account ,, Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null,对于列:NHibernate.Mapping.Column(AccountCode)
mapping.References(b => b.Account).Column("Code");
Run Code Online (Sandbox Code Playgroud)
结果Invalid column name 'Code'..
和:
mapping.References(b => b.Account).Column("AccountCode");
mapping.References(b => b.Account).Column("AccountCode").Access.Property();
Run Code Online (Sandbox Code Playgroud)
覆盖我的所有IReferenceConvention覆盖(将某些类映射为具有<class name>Code键列).
尝试时HasMany:
mapping.HasMany<Account>(b => b.Account).KeyColumn("AccountCode");
Run Code Online (Sandbox Code Playgroud)
自定义类型不实现UserCollectionType:.Account
// in BeneficiaryMapping
mapping.References(b => b.Account)
.Column("AccountCode" /* of Beneficiary table*/)
.PropertyRef(a => a.Code); // use the Column of Code as the joincolumn in Account table
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2823 次 |
| 最近记录: |