【问题标题】:Map to view on custom field name - one-to-one relationship映射以查看自定义字段名称 - 一对一关系
【发布时间】:2012-10-25 20:00:39
【问题描述】:

我无法让 NHibernate 在这种一对一关系中映射BeneficiaryAccountCode 列的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 列。

我尝试了MapReferencesJoin(告诉我连接已经存在)和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");

生成的 SQL 使用 Beneficiary.Id 字段而不是 Beneficiary.AccountCode。 (在你问之前,我使用“none”,因为Account 是一个视图,它不能有键)。

mapping.Join("AccountCode", x => x.References(y => y.Account));
mapping.Join("Account", b => b.KeyColumn("AccountCode"));

结果为@​​987654342@。

还有:

mapping.Map(b => b.Account, "AccountCode");
mapping.Map(b => b.Account).ReadOnly().Column("AccountCode");

结果:

无法确定以下的类型:.Account、、Version=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

【问题讨论】:

  • 版本(如果重要):NHibernate 3.3.1.4000、FluentNHibernate 1.3.0.733。

标签: view fluent-nhibernate sql-server-2000


【解决方案1】:
// 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

【讨论】:

    【解决方案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");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 2014-02-10
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      相关资源
      最近更新 更多