流利的nhibernate PrimaryKeyConvention的属性名称 [英] fluent nhibernate PrimaryKeyConvention for property names
问题描述
这个问题已经提出来了,但没有合适的答案.
This question has been asked already but there was no decent answer.
是否有流利的nhibernate的属性名称约定,以便寻找ProductId
而不是寻找Id
?
Is there a property name convention for fluent nhibernate so that instead of looking for Id
it looks for ProductId
?
PrimaryKeyConvention
适用于数据库中的列名,而不适用于属性名.
PrimaryKeyConvention
applies to the column names in the database, NOT the property names.
考虑这种情况:
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方法映射您的身份属性.例如:Id(x => x.Id).
FluentNHibernate.Visitors.ValidationException: The entity 'Product' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id).
我可以添加/覆盖哪种约定来告诉流利的nhibernate查找'EntityName'+"Id"?在属性名称中?我看过此约定页面,但没有找到了要覆盖的那个.
What convention can I add/override to tell fluent nhibernate to look for 'EntityName'+"Id" in the property names? I've looked at this convention page but havent found the one to override.
推荐答案
public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool IsId(Member member)
{
return member.Name == member.DeclaringType.Name + "Id";
}
}
这篇关于流利的nhibernate PrimaryKeyConvention的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!