C# 属性名称的fluent nhibernate PrimaryKeyConvention
这个问题一直存在,但没有像样的答案 fluent nhibernate是否有属性名称约定,以便它不查找C# 属性名称的fluent nhibernate PrimaryKeyConvention,c#,fluent-nhibernate,C#,Fluent Nhibernate,这个问题一直存在,但没有像样的答案 fluent nhibernate是否有属性名称约定,以便它不查找Id,而是查找ProductId PrimaryKeyConvention适用于数据库中的列名,而不是属性名 考虑以下情况: public class Product { public int ProductId { get; set; } public string Name { get; set; } } public class AutomappingConfigurat
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();
}
公共类产品
{
public int ProductId{get;set;}
公共字符串名称{get;set;}
}
公共类自动映射配置:默认自动映射配置
{
公共覆盖布尔ShouldMap(类型)
{
bool shouldMap=type.In(typeof(Product));
返回shouldMap;
}
}
public void TestFluentNHibernate()
{
var configuration=fluntly.Configure()。
数据库(MsSqlConfiguration.MsSql2008.ConnectionString(“数据库=asd;服务器=。\\sqlexpress;可信连接=true;”)
.Mappings(m=>
{
IAutomappingConfiguration cfg=新的自动映射配置();
m、 Add(AutoMap.AssemblyOf(cfg.Conventions.AddFromAssemblyOf());
});
var factory=configuration.BuildSessionFactory();
}
结果:
FluentNHibernate.Visitors.ValidationException:实体“产品”没有映射的Id。使用Id方法映射标识属性。例如:Id(x=>x.Id)
我可以添加/覆盖什么约定来告诉fluent nhibernate在属性名称中查找“EntityName”+“Id”?我已经查看了约定页面,但还没有找到要覆盖的页面
public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool IsId(Member member)
{
return member.Name == member.DeclaringType.Name + "Id";
}
}