【问题标题】:Fluent NHibernate automap configuration throwing vague errorFluent NHibernate 自动映射配置抛出模糊错误
【发布时间】:2011-07-24 10:59:45
【问题描述】:

我正在尝试 Fluent NHibernate 的自动映射功能,当我尝试将其移至自动映射时,在构建 SessionFactory 时,使用显式 ClassMap 配置的相同代码失败。

代码如下:

public static ISessionFactory GetSessionFactory()
{
    if (_sessionFactory == null)
    {
        _sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("DB")))
            // It works with the following:
            // .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Customer>())
            // It fails with this:
            .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Customer>()))
            .BuildSessionFactory();

    return _sessionFactory;
}

我得到的错误是:

配置无效或不完整 在创建一个 会话工厂。检查潜在原因 集合和 InnerException 更详细。

PotentialReasons 得到Count = 0,内部异常与上面相同。

stacktrace 指的是:

在 FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() 在 d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:113 行

似乎我已经尽一切努力让它工作,而我最接近的就是让初始化工作在我尝试使用会话时得到一个Could not find persister for... 错误,我什至没有记住我是如何做到这一点的。

我正在使用带有 NHibernate 3.0、SQL 2008 数据库的 build #694。任何想法我做错了什么?

【问题讨论】:

  • 嵌套内部异常通常有3-4层。你都检查过了吗?
  • 哇...我现在感觉自己像个白痴!您是绝对正确的-当我深入研究时,我发现这是映射枚举的问题。在我的显式映射中,我刚刚做了一个CustomType(typeof(MyEnum)),但我必须看看自动映射时这是如何实现的......

标签: c# fluent-nhibernate automapping


【解决方案1】:

这是 Aliostad 的评论帮助我找到的愚蠢错误。我有一个枚举类型,它作为整数存储在数据库中,而 NHibernate 对此感到窒息。我在设置中添加了一个 EnumConvention,如下所示:

_sessionFactory = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("DB")))
    .Mappings(
        m =>
        m.AutoMappings.Add(AutoMap.AssemblyOf<Customer>(new AutomapConfiguration()).Conventions.
                               Setup(c =>
                                         {
                                             c.Add<PrimaryKeyConvention>();
                                             c.Add<EnumConvention>();
                                             c.Add<CascadeAllConvention>();
                                         })
                               .IgnoreBase(typeof (EntityBase<>))
                               .OverrideAll(map => map.IgnoreProperty("IsValid"))))
    .BuildSessionFactory();

这是枚举约定:

public class EnumConvention : IUserTypeConvention
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Property.PropertyType.IsEnum);
    }

    public void Apply(IPropertyInstance target)
    {
        target.CustomType(target.Property.PropertyType);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多