NHibernate配置问题

发布于 2024-11-03 03:50:27 字数 1932 浏览 0 评论 0原文

任何人都可以帮助我吗?我的目标是始终使用相同的数据库。对我来说,它会覆盖我的所有数据。我收到此错误:创建 SessionFactory 时使用了无效或不完整的配置。检查 PotentialReasons 集合和 InnerException 了解更多详细信息。

我的代码如下所示:

使用 FluentNHibernate;使用 NHibernate;使用 FluentNHibernate.Cfg;使用 FluentNHibernate.Cfg.Db;使用 FluentNHibernate.Automapping;使用 NHibernate.Cfg;使用 NHibernate.Tool.hbm2ddl;使用 NHibernate.Criterion;使用 FluentNhibernate测试;使用 FluentNHibernate.Mapping;使用 MMAdminPfyn.MappingFiles;

命名空间 FluentNhibernateTest { 公共密封类 FluentNHibernateHelper { 私有静态ISessionFactory sessionFactory;

 public static ISessionFactory GetInstance()
    {
        if (sessionFactory == null)
        {
            sessionFactory = BuildSessionFactory();
        }
        返回会话工厂;
    }

    私有静态 ISessionFactory BuildSessionFactory()
    {

        返回 Fluently.Configure()
            .数据库(PostgreSQL配置.PostgreSQL82
            .ConnectionString(c => c
                .主机(“本地主机”)
                .端口(5432)
                .数据库(“blablabla”)
                .用户名(“blablabla”)
                .密码(“blablabla”)))
                .Mappings(m => m.FluentMappings
                                    .AddFromAssemblyOf()
                                    .AddFromAssemblyOf()
                                    .AddFromAssemblyOf()
                                    .AddFromAssemblyOf()
                                    .AddFromAssemblyOf()
                                    .AddFromAssemblyOf()
                        )
            .ExposeConfiguration(构建架构)
            .BuildSessionFactory();
    }

    私有静态无效BuildSchema(配置config)
    {
        新的 SchemaExport(config).Create(true,

真); } } }

Can anybody help me. My aim is, to use always the same database. By me it overrides all my data. I get this error: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

My Code look like:

using FluentNHibernate; using
NHibernate; using
FluentNHibernate.Cfg; using
FluentNHibernate.Cfg.Db; using
FluentNHibernate.Automapping; using
NHibernate.Cfg; using
NHibernate.Tool.hbm2ddl; using
NHibernate.Criterion; using
FluentNhibernateTest; using
FluentNHibernate.Mapping; using
MMAdminPfyn.MappingFiles;

namespace FluentNhibernateTest {
public sealed class FluentNHibernateHelper
{
private static ISessionFactory sessionFactory;

    public static ISessionFactory GetInstance()
    {
        if (sessionFactory == null)
        {
            sessionFactory = BuildSessionFactory();
        }
        return sessionFactory;
    }

    private static ISessionFactory BuildSessionFactory()
    {

        return Fluently.Configure()
            .Database(PostgreSQLConfiguration.PostgreSQL82
            .ConnectionString(c => c
                .Host("localhost")
                .Port(5432)
                .Database("blablabla")
                .Username("blablabla")
                .Password("blablabla")))
                .Mappings(m => m.FluentMappings
                                    .AddFromAssemblyOf<AdresseMap>()
                                    .AddFromAssemblyOf<PersonMap>()
                                    .AddFromAssemblyOf<InstitutionMap>()
                                    .AddFromAssemblyOf<LiteraturMap>()
                                    .AddFromAssemblyOf<KategorieMap>()
                                    .AddFromAssemblyOf<MediaDateiMap>()
                        )
            .ExposeConfiguration(BuildSchema)
            .BuildSessionFactory();
    }

    private static void BuildSchema(Configuration config)
    {
        new SchemaExport(config).Create(true,

true);
}
} }

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

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

发布评论

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

评论(2

盛夏已如深秋| 2024-11-10 03:50:27

下面的内容看起来很奇怪:

.Mappings(m => m.FluentMappings
                                    .AddFromAssemblyOf<AdresseMap>()
                                    .AddFromAssemblyOf<PersonMap>()
                                    .AddFromAssemblyOf<InstitutionMap>()
                                    .AddFromAssemblyOf<LiteraturMap>()
                                    .AddFromAssemblyOf<KategorieMap>()
                                    .AddFromAssemblyOf<MediaDateiMap>()
                        )

这些映射(AddressMap、PersonMap 等)都存在于不同的 dll 中吗?如果没有,您只需要这些语句之一,它就会在该程序集中找到您的所有映射。所以你只需要以下内容:

return Fluently.Configure()
            .Database(PostgreSQLConfiguration.PostgreSQL82
            .ConnectionString(c => c
                .Host("localhost")
                .Port(5432)
                .Database("blablabla")
                .Username("blablabla")
                .Password("blablabla")))
                .Mappings(m => m.FluentMappings
                                    .AddFromAssemblyOf<AdresseMap>()
                        )
            .ExposeConfiguration(BuildSchema)
            .BuildSessionFactory();

The following looks odd:

.Mappings(m => m.FluentMappings
                                    .AddFromAssemblyOf<AdresseMap>()
                                    .AddFromAssemblyOf<PersonMap>()
                                    .AddFromAssemblyOf<InstitutionMap>()
                                    .AddFromAssemblyOf<LiteraturMap>()
                                    .AddFromAssemblyOf<KategorieMap>()
                                    .AddFromAssemblyOf<MediaDateiMap>()
                        )

Do those mappings (AddressMap, PersonMap, etc) all exist in different dlls? If they don't you only need one of those statements and it will find all of your mappings in that assembly. So you would only need the following:

return Fluently.Configure()
            .Database(PostgreSQLConfiguration.PostgreSQL82
            .ConnectionString(c => c
                .Host("localhost")
                .Port(5432)
                .Database("blablabla")
                .Username("blablabla")
                .Password("blablabla")))
                .Mappings(m => m.FluentMappings
                                    .AddFromAssemblyOf<AdresseMap>()
                        )
            .ExposeConfiguration(BuildSchema)
            .BuildSessionFactory();
爱*していゐ 2024-11-10 03:50:27

这个问题与此有关:

.Mappings(m => m.FluentMappings
                                .AddFromAssemblyOf<AdresseMap>()
                                .AddFromAssemblyOf<PersonMap>()
                                .AddFromAssemblyOf<InstitutionMap>()
                                .AddFromAssemblyOf<LiteraturMap>()
                                .AddFromAssemblyOf<KategorieMap>()
                                .AddFromAssemblyOf<MediaDateiMap>()
                    )

引用自 https://github.com/jagregory/ fluent-nhibernate/wiki/Fluent-configuration,

如果您的应用程序专门使用
流畅的映射,那么这就是适合您的配置。

这意味着您正在提供包含类的程序集;你在这里所做的相当于告诉 Fluent,“映射包含此类的程序集”。

var sessionFactory = Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =>
    m.FluentMappings
      .AddFromAssemblyOf<YourEntity>())
  .BuildSessionFactory();

试试这个吧。

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<AdresseMap>());

This problem is with this bit:

.Mappings(m => m.FluentMappings
                                .AddFromAssemblyOf<AdresseMap>()
                                .AddFromAssemblyOf<PersonMap>()
                                .AddFromAssemblyOf<InstitutionMap>()
                                .AddFromAssemblyOf<LiteraturMap>()
                                .AddFromAssemblyOf<KategorieMap>()
                                .AddFromAssemblyOf<MediaDateiMap>()
                    )

Quoted from https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-configuration,

If you're in the situation where your application is exclusively using
fluent mappings, then this is the configuration for you.

which means that you are giving the assembly which contains the classes; what you're doing here is equal to telling Fluent, "Map the assembly which contains this class."

var sessionFactory = Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =>
    m.FluentMappings
      .AddFromAssemblyOf<YourEntity>())
  .BuildSessionFactory();

Try this instead.

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