简体   繁体   English

使用 ModelMapper for NHibernate 通过代码映射

[英]using ModelMapper for NHibernate with mapping by code

I created a HibernateMapping in seperated Classes.我在单独的类中创建了一个 HibernateMapping。 The Mapping and classes are in one Assembly.映射和类在一个程序集中。 The NUnit tests which I use to test this are in an other Assembly.我用来测试的 NUnit 测试在另一个程序集中。 I think I did it like in this post this我想我在这篇文章中做到了这个

class testMapping : ClassMapping<test>{
//myMappings
}

Now I created a mapper and added it to my configuration:现在我创建了一个映射器并将其添加到我的配置中:

var cfg=new Configuration();
cfg.Configure();
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntries());
var session = cfg.BuildSessionFactory();

but my Mapping is not found.但我的映射没有找到。 Do I replace the mapper.AddMappings() with:我是否将mapper.AddMappings()替换为:

mapper.AddMappings(New List<SystemType> {typeof(testMapping)});

it will work.它会起作用。

Do I use the GetExecutingAssembly wrong?我是否使用 GetExecutingAssembly 错误?

I dont want to add ALL my Mappings seperately.我不想单独添加我的所有映射。

You can add one class to the Addmappings from the assembly that it belongs to.您可以将一个类从它所属的程序Addmappings添加到Addmappings

My config is like this:-我的配置是这样的:-

mapper.AddMappings(typeof(CmsMeta).Assembly.GetTypes());

Basically I have 50 or so mapping classes but only need to specify the assembly where one of them lives.基本上我有 50 个左右的映射类,但只需要指定其中一个所在的程序集。 NHibernate will auto scan that assembly to find the rest. NHibernate 将自动扫描该程序集以找到其余的程序集。 All my mapping and model classes live in a single separate assembly Domain.Model我所有的映射和​​模型类都存在于一个单独的程序集Domain.Model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM