1
public class TransactionIdentityModelMapping : ClassMap<TransactionIdentityModel> 
{ 
    public TransactionIdentityModelMapping() 
    { 
     Table("TransactionIdentities"); 
     Id(x => x.Id); 
     References(x => x.Transaction); 
    } 
} 

public class TransactionModelMapping : ClassMap<TransactionModel> 
{ 
    public TransactionModelMapping() 
    { 
     Table("Transactions"); 
     Id(x => x.Id); 
     HasMany(x => x.Identities); 
    } 
} 

TransactionIdentityModel.Transaction的類型爲TransactionModel。這意味着有一個名爲"TransactionModel_id"的列。我如何更改此列名稱?指定外鍵列的名稱

我曾嘗試:

References(x => x.Transaction).Column("Transaction_id"); 

和:

References(x => x.Transaction).ForeignKey("Transaction_id"); 

和:

References(x => x.Transaction).Column("Transaction_id").ForeignKey("Id").Fetch.Join(); 

這些都產生了2列,一個我想要的( 「TRANSACTION_ID」)和原始(「TransactionModel_id」)。

我是否還需要對HasMany做些什麼?

編輯型號:

public class TransactionModel 
{ 
    /// <summary> 
    /// For NHibernate 
    /// </summary> 
    protected TransactionModel() 
    { 
    } 

    public static TransactionModel FromId(TransactionIdentityModel tranIdentity) 
    {  
     return new TransactionModel 
      { 
       Identities = new List<TransactionIdentityModel> { tranIdentity } 
      }; 
    } 

    public virtual Guid Id { get; protected set; } 

    public virtual IList<TransactionIdentityModel> Identities { get; protected set; } 
} 

public class TransactionIdentityModel 
{   
    public virtual Guid Id { get; protected set; } 

    public virtual TransactionModel TransactionModel { get; set; } 
} 

屈服2列,這是,如果我使用地圖生成的SQL: References(x => x.Transaction).Column("Transaction_id").ForeignKey("Id").Fetch.Join();

alter table TransactionIdentities 
    add constraint Id 
    foreign key (Transaction_id) 
    references Transactions 

alter table TransactionIdentities 
    add constraint FK958B77026F5C4B80 
    foreign key (TransactionModel_id) 
    references Transactions 

回答

1

假設Transaction_Id是外鍵相關TransactionModel那麼您需要將您的參考聲明更改爲:

References(x => x.Transaction).Column("Transaction_id"); 

這將覆蓋nHibernates默認期望的領域是TransactionModel_id

在這個問題Prevent Nhibernate schemaexport from generating foreign key constraints on has many relationship看,我認爲你將不得不改變你的HasMany映射到這一點:

HasMany(x => x.Identities).KeyColumn("Transaction_id") 
+0

沒有工作我結束了兩個,一個叫做「Transaction_id」,一個叫做「TransactionModel_id」。有什麼我需要做的''ClassMap '它有一個'HasMany'鏈接? – weston

+0

查看編輯問題的'HasMany'方。 – weston

+0

@weston你可以發佈你的類定義'TransactionModel'和'TransactionIdentityModel'請 – CSL