2
votes

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

2
Did you check what the inner exception says?Linkgoron
It says me: {"Could not compile the mapping document: (XmlDocument)"}user721654
And that: Duplicate class/entity mapping MMAdminPfyn.Objects.Audio (one the Browser)user721654

2 Answers

0
votes

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();
0
votes

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