2011-12-15 65 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"; 
    } 
}