c# - "Import"通过代码 ClassMapping 到 Fluent NHibernate

标签 c# nhibernate fluent-nhibernate

我想使用NHibernate.Asp.Identity NHibernate 库,我想扩展标准 IdentityUser类 - 但是此库中编写的所有映射都是通过 ByCode.Conformist.ClassMapping<> 完成的。有什么方法可以“导入”这些映射(也许在数据库配置期间?),这样我就可以映射我的 ApplicationUserIdentity像 Fluent 这样:

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

哪里ApplicationUser是:

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

并映射到 IdentityUser写有ByCode.Conformist.ClassMapping<>

最佳答案

我想说,这不应该是一个问题。我们最终需要的是 Configuration 对象 - 提供来自 Fluent 的(甚至更多) 映射 1) 以及 2 )来自按代码映射。基于这些资源:

这可能是配置:

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

扩展:

不确定如何通过代码混合流利和映射以实现继承...

但是还有更狭窄的方法如何流畅地配置并注入(inject)一些按代码映射部分:

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

关于c# - "Import"通过代码 ClassMapping 到 Fluent NHibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25339918/

相关文章:

c# - 负向后视的小数或整数

c# - ASP.NET MVC 1.0 + 单声道 2.4

c# - 假期 C# 的日期约定

c# - 需要帮助来理解这个 C# 泛型类

nhibernate - 使用 FluentNHibernate 映射通用 EntityBase<TEntity> 类

sql-server - NHibernate 与 SQL Server 全文

c# - 获取变量或参数的名称

nhibernate - 域模型–存储库–跨子系统的通信

c# - ASP.NET Identity - 无法注册 UserTokenProvider

fluent-nhibernate - 带有额外列的 Fluent Nhibernate 多对多映射