1
我想遷移到流利的NHibernate和有以下問題:(功能NHibernate自動映射PrimaryKey屬性名稱不是「ID」
我有AA號碼的叫CompanyXXXXXX的方式類,它們都有一個的PrimaryKey「CompanyId」,它是類型公司的
其中我使用直到名字是甲HBM映射文件這樣的:
<class name="CompanyAccounting" table="Company_Accounting" >
<id column="CompanyID" type="Int32">
<generator class="foreign">
<param name="property">Company</param>
</generator>
</id>
<one-to-one name="Company" constrained="true" />
</class>
實體如下:
public class CompanyAccounting
{
public virtual Company Company {get;set;}
}
是否可以使用某種類型的AutoMapping功能,因爲我有一打這些類,並且可能會有更多。
我曾嘗試以下:
public class CustomPrimaryKeyConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
var type = instance.EntityType;
if (type.Name.StartsWith("Company") && type.Name.Length > 7)
{
instance.CustomType(typeof(Company));
instance.Column("CompanyId");
}
else
{
instance.Column(instance.EntityType.Name + "Id");
}
}
}
編輯:但是我如果(......)對於 「CompanyAccounting」 類型]甚至沒有被擊中。有什麼建議麼?
例外:
The entity 'CompanyAccounting' doesn't have an Id mapped.
Use the Id method to map your identity property. For example: Id(x => x.Id).
喜忘了提,它不被打到了CompanyAccounting的東西,它被擊中了前面,班,但它不調用該方法申請CompanyAccounting,異常得到事先命中。 – 2011-05-19 13:05:52
因此,您的約定適用於其他課程,但不適用於公司會計? CompanyAccounting與它所用的類相同嗎? – 2011-05-19 14:20:49
是的...我認爲這個問題是因爲CompanyAccounting沒有公開Id {get; set;},但我不知道如何告訴automapper不要查找Id字段,而是使用Company字段。 – 2011-05-19 14:36:29