流利的nhibernate PrimaryKeyConvention的属性名称 [英] fluent nhibernate PrimaryKeyConvention for property names

查看:84
本文介绍了流利的nhibernate PrimaryKeyConvention的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经提出来了,但没有合适的答案.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆