2011-12-15 68 views
0

此问题已被asked already,但没有像样的答案。流利nhibernate PrimaryKeyConvention为属性名称

是否有属性名约定功能NHibernate以便不用找Id它看起来ProductId

PrimaryKeyConvention适用于数据库中的列名称,而不是属性名称。

考虑这种情况:

public class Product 
{ 
    public int ProductId { get; set; } 
    public string Name { get; set; } 
} 

public class AutomappingConfiguration : DefaultAutomappingConfiguration 
{ 
    public override bool ShouldMap(Type type) 
    { 
     bool shouldMap = type.In(typeof(Product)); 
     return shouldMap; 
    } 
} 

public void TestFluentNHibernate() 
{ 
    var configuration = Fluently.Configure(). 
     Database(MsSqlConfiguration.MsSql2008.ConnectionString("database=asd;server=.\\sqlexpress;trusted_connection=true;")) 
     .Mappings(m => 
         { 
          IAutomappingConfiguration cfg = new AutomappingConfiguration(); 
          m.AutoMappings.Add(AutoMap.AssemblyOf<Product>(cfg).Conventions.AddFromAssemblyOf<PrimaryKeyConvention>()); 
         }); 

    var factory = configuration.BuildSessionFactory(); 
} 

结果:

FluentNHibernate.Visitors.ValidationException:实体 '产品' 不具有标识映射。使用Id方法来映射您的身份属性。例如:Id(x => x.Id)。

什么约定可以添加/重写告诉流利的nhibernate在属性名称中查找'EntityName'+“Id”?我看过this会议页面,但没有找到一个重写。

回答

1
public class AutomappingConfiguration : DefaultAutomappingConfiguration 
{ 
    public override bool IsId(Member member) 
    { 
     return member.Name == member.DeclaringType.Name + "Id"; 
    } 
}