2011-05-19 134 views
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). 

回答

1

你已经注册与功能NHibernate该公约?

你应该沿线

AutoMap.AssemblyOf<CompanyAccounting>() 
     .Conventions.AddFromAssemblyOf<CustomPrimaryKeyConvention>() 
+0

喜忘了提,它不被打到了CompanyAccounting的东西,它被击中了前面,班,但它不调用该方法申请CompanyAccounting,异常得到事先命中。 – 2011-05-19 13:05:52

+0

因此,您的约定适用于其他课程,但不适用于公司会计? CompanyAccounting与它所用的类相同吗? – 2011-05-19 14:20:49

+0

是的...我认为这个问题是因为CompanyAccounting没有公开Id {get; set;},但我不知道如何告诉automapper不要查找Id字段,而是使用Company字段。 – 2011-05-19 14:36:29