fluent nhibernate PrimaryKeyConvention用于属性名
本文关键字:属性 用于 PrimaryKeyConvention nhibernate fluent | 更新日期: 2023-09-27 18:17:49
这个问题已经被问过了,但是没有合适的答案。
是否有一个属性名约定为流利的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:实体'Product'没有映射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";
}
}