3

我无法让 NHibernate在这种一对一关系中映射表列AccountCode的列(每个都有一个,每个都有一个)。BeneficiaryAccountCodeAccountBeneficiaryBeneficiaryAccount

课程:

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我尝试了,的不同变体ReferencesJoin它告诉我连接已经存在)和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

4

2 回答 2

7
// 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
于 2012-10-22T12:19:36.917 回答
2

这种方法类似于 Firo 的方法,但映射更简单。它References像他建议的那样使用,因为这就是 NH 允许您选择实体主键属性以外的属性来引用另一种类型的方式。PropertyRef没有必要,因为 NH“知道”它将使用该值AccountCode作为Account视图中的“键”。

以下是如何映射Account

public class AccountMap : ClassMap<Account>
{
    public AccountMap()
    {
        // Code as a psuedo primary key in the view:
        Id(acc => acc.Code)
            .GeneratedBy.Assigned();

        Map(acc => acc.Name);

        // Add other mappings here...

        // Ensure NH doesn't try to update the lookup view:
        ReadOnly();
    }
}

这是Beneficiary外观的映射方式:

public class BeneficiaryMap : ClassMap<Beneficiary>
{
    public BeneficiaryMap()
    {
        Id(b => b.Id)
            .GeneratedBy.Identity()
            .UnsavedValue(0);

        Map(b => b.Name);
        // Other mapped properties...

        References<BeneficiaryGroup>(b => b.Group, "BeneficiaryGroupId");
        References<Account>(b => b.Account, "AccountCode");
    }
}
于 2012-10-22T15:52:54.223 回答