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