我想使用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/