【问题标题】:fluent nhibernate PrimaryKeyConvention for property names用于属性名称的流利的 nhibernate PrimaryKeyConvention
【发布时间】:2011-12-15 01:01:35
【问题描述】:

这个问题一直是asked already,但没有合适的答案。

流畅的 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 方法映射您的身份属性。例如:Id(x => x.Id)。

我可以添加/覆盖什么约定来告诉 fluent nhibernate 在属性名称中查找“EntityName”+“Id”?我查看了this 约定页面,但没有找到要覆盖的页面。

【问题讨论】:

    标签: c# fluent-nhibernate


    【解决方案1】:
    public class AutomappingConfiguration : DefaultAutomappingConfiguration
    {
        public override bool IsId(Member member)
        {
            return member.Name == member.DeclaringType.Name + "Id";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多