Fluent NHibernate 自动映射配置抛出模糊错误

发布于 2024-10-27 10:35:59 字数 1324 浏览 0 评论 0原文

我正在尝试 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 更多细节。

我得到 PotentialReasonsCount = 0 ,内部异常与上面相同。

堆栈跟踪指的是:

在 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。有什么想法我做错了吗?

I'm trying out the automapping capability of Fluent NHibernate, and the same code that worked with explicit ClassMap configurations is failing when building the SessionFactory when I try to move it to automapping.

Here's the code:

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;
}

The error I get is:

An invalid or incomplete configuration
was used while creating a
SessionFactory. Check PotentialReasons
collection, and InnerException for
more detail.

I get Count = 0 for PotentialReasons and the inner exception is the same as above.

The stacktrace refers to:

at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
in d:\Builds\FluentNH-v1.x-nh3\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 113

It seems like I've tried everything to get it to work, and the closest I came was to have the initialization work only to get a Could not find persister for... error when I tried to use the session, and I don't even remember how I was able to get that to happen.

I'm using the build #694 with NHibernate 3.0, SQL 2008 database. Any ideas what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素染倾城色 2024-11-03 10:35:59

这是一个愚蠢的错误,阿利奥斯塔的评论帮助我找到了。我有一个枚举类型,它作为整数存储在数据库中,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);
    }
}

This was a dumb error that Aliostad's comment helped me find. I had an enum type that was stored as an integer in the database, and NHibernate was choking on that. I added an EnumConvention to the setup like this:

_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();

Here's that enum convention:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文