2012-10-19 46 views
3

我無法得到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。 (之前你問,我使用「無」,因爲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,文化=中立,公鑰=空,爲列: 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。賬戶

+0

版本中,如果重要的:NHibernate的3.3.1.4000,FluentNHibernate 1.3.0.733。 – Oded

回答

6
// 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允許您選擇除實體主鍵屬性之外的其他屬性來引用另一種類型的屬性。由於NH「知道」它將使用AccountCode值作爲Account視圖中的「關鍵字」,所以PropertyRef不是必需的。

這裏是如何映射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"); 
    } 
} 
相關問題