2
votes

I'd like to use NHibernate.Asp.Identity library for NHibernate, I'd like to extend standard IdentityUser class - however all mappings written in this library are done with ByCode.Conformist.ClassMapping<>. Is there any way that I could "import" these mappings (maybe during database configuration?) so I could map my ApplicationUserIdentity with Fluent like that:

public class ApplicationUserIdentityMap : ClassMap<ApplicationUser>
{
     public ApplicationUserIdentityMap()
     {
          HasMany(x => x.MyProp);
          ...
     }
}

where ApplicationUser is:

public class ApplicationUser : IdentityUser
{
    ... my properties
}

and mappings to IdentityUser are written with ByCode.Conformist.ClassMapping<>

1

1 Answers

3
votes

I would say, that this should not be an issue. What we at the end need - is the Configuration object - provided with both (or even more) mapping 1) from Fluent as well as 2) from Mapping-by-code. Based on these resources:

This could be the configuration:

// standard Fluent configuration
var fluentConfig = FluentNHibernate.Cfg.Fluently.Configure()
    .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
        .ConnectionString(@"Data Source=.;Database=TheCoolDB;Trusted_Connection=yes;"))
    .Mappings(m =>
        m.FluentMappings
            .AddFromAssemblyOf<MyAwesomeEntityMap>());

// return the NHibernate.Cfg.Configuration
var config = fluentConfig
    .BuildConfiguration();

// now Mapping by Code

// firstly the Mapper
var mapper = new NHibernate.Mapping.ByCode.ModelMapper();

mapper.AddMappings(Assembly
    .GetAssembly(typeof(IdentityUser)) // here we have to get access to the mapping
    .GetExportedTypes());

// finally the mapping just read
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

// extend already existing mapping
config.AddDeserializedMapping(mapping, null);

// the ISessionFactory
var factory = config.BuildSessionFactory();

Extend:

Not sure how to mix the fluent and mapping by code for purposes of inheritance...

But there is even more narrow way how to configure fluently and inject some mapping-by-code parts:

// Let's start with mapping-by-code,
// so firstly create the Mapper
var mapper = new NHibernate.Mapping.ByCode.ModelMapper();

mapper.AddMappings(Assembly
    .GetAssembly(typeof(IdentityUser)) // here we have to get access to the mapping
    .GetExportedTypes());
// create the mapping
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

// standard Fluent configuration
var fluentConfig = FluentNHibernate.Cfg.Fluently.Configure()
    .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
        .ConnectionString(@"Data Source=.;Database=TheCoolDB;Trusted_Connection=yes;"))
    .Mappings(m =>
        m.FluentMappings
            .AddFromAssemblyOf<MyAwesomeEntityMap>())

// here we INJECT the HBM mapping
// via call to built in ExposeConfiguration(Action<Configuration> config)
     .ExposeConfiguration(c => c.AddDeserializedMapping(mapping, null));

// the ISessionFactory
var factory = fluentConfig
    .BuildSessionFactory();