Question

I don't know if this is NHibernate in general, or fluent... basically I'm trying to make it create a database based on the mappings.

Originally, I tried a Postgres connection, but that wasn't working, so I thought I'd switch to an in memory SQLite one, this didn't work either.

I'm assuming there is something fundamentally wrong with my assumptions on how to make this work, or when it works.

here is the code...

if (_sessionFactory == null)
{
    FluentConfiguration configuration;
    var rawConfig = new NHibernate.Cfg.Configuration();
    configuration = Fluently.Configure(rawConfig)
      .Database(SQLiteConfiguration.Standard.InMemory())
      .Cache(c => c.ProviderClass<SysCacheProvider>().UseSecondLevelCache())
      .ExposeConfiguration(cfg =>
        {
            cfg.SetProperty("current_session_context_class", "web");
            cfg.AddMapping(MapIdentityModels());
            var schema = new SchemaUpdate(cfg);
            schema.Execute(true, true);
        });

    _sessionFactory = configuration.BuildSessionFactory();
 }
return _sessionFactory;


    private static HbmMapping MapIdentityModels()
    {
        var baseEntityToIgnore = new[] { 
            typeof(NHibernate.AspNet.Identity.DomainModel.EntityWithTypedId<int>), 
            typeof(NHibernate.AspNet.Identity.DomainModel.EntityWithTypedId<string>), 
        };

        var allEntities = new[] { 
            typeof(IdentityUser), 
            typeof(ApplicationUser), 
            typeof(IdentityRole), 
            typeof(IdentityUserLogin), 
            typeof(IdentityUserClaim), 
        };

        var mapper = new ConventionModelMapper();
        DefineBaseClass(mapper, baseEntityToIgnore);
        mapper.IsComponent((type, declared) => typeof(NHibernate.AspNet.Identity.DomainModel.ValueObject).IsAssignableFrom(type));

        mapper.AddMapping<IdentityUserMap>();
        mapper.AddMapping<IdentityRoleMap>();
        mapper.AddMapping<IdentityUserClaimMap>();

        return mapper.CompileMappingFor(allEntities);
    }

    private static void DefineBaseClass(ConventionModelMapper mapper, System.Type[] baseEntityToIgnore)
    {
        if (baseEntityToIgnore == null) return;
        mapper.IsEntity((type, declared) =>
            baseEntityToIgnore.Any(x => x.IsAssignableFrom(type)) &&
            !baseEntityToIgnore.Any(x => x == type) &&
            !type.IsInterface);
        mapper.IsRootEntity((type, declared) => baseEntityToIgnore.Any(x => x == type.BaseType));
    }

I'm getting an error message when anything tries to use the database that it doesn't exist or the table doesn't exist within it.

My goal here is to have the code create tables in a Postgres database on first run.

UPDATE

I've managed to fix this by changing the line

 cfg.AddMapping(MapIdentityModels())

to

 cfg.AddDeserializedMapping(MapIdentityModels())

Not sure why, but this works, if someone can explain why, and potentially how to achieve the same result without using the ExposeConfiguration route, then I'll mark it as the answer.

Was it helpful?

Solution

It seems that the issue is to do with the mappings not being added properly, so it probably was trying to create it but there weren't any mappings to create.

To fix it, I added the mappings using:

cfg.AddDeserializedMapping(MapIdentityModels())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top