流畅的nhibernate配置不工作 [英] fluent nhibernate configuration not working
问题描述
解决:在配置代码时使用ClassMap,在配置文件中配置ClassMapping,但在代码中进行映射。
Solved: ClassMap when configuration in code, ClassMapping when configuration in config file, but mapping in code.
当尝试将Object1类型的对象保存到我的数据库我收到错误:
When trying to save an object of type Object1 to my database I get the error:
不存在:Object1。
No persister for: Object1.
b $ b
类定义:
Class definition:
public class Object1
{
public virtual Guid Id { get; protected set; }
public virtual DateTime Created { get; protected set; }
public virtual DateTime LastModified { get; protected set; }
public virtual Other Reference { get; set; }
}
映射:
public class Object1Mapping : ClassMapping<Object1>
{
public Object1Mapping()
{
Id(p => p.Id);
Property(p => p.Created);
Property(p => p.LastModified);
ManyToOne(p => p.Reference, m => m.Cascade(Cascade.Remove));
}
}
配置代码:
sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(c => c
.Server("serverUrl")
.Database("myMsSqlDatabase")
.Username("sa")
.Password("pasword1")))
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<Object1Mapping>()
.AddFromAssemblyOf<Object1>()
.BuildSessionFactory();
我的所有类都是公共的,我为每个需要持久化的类添加了 AddFromAssemblyOf
添加类本身和映射类我知道一个 AddFromAssembly
应该足够,因为应该从该程序集添加所有的映射类,所有我的映射类都在一个项目中文件夹。
All my classes are public and I added an AddFromAssemblyOf
for every class that needs a persister. Also I tried both adding the classes themselves and the mapping classes. I know that one AddFromAssembly
should be enough as should add all mapping classes from that assembly and all my mapping classes are in one project in one folder.
我设法运行诊断程序,它通过正确的程序集,但没有发现映射,这是结果:
I managed to run diagnostics and it's looking through the correct Assembly, but it discovered no mappings, here is the result:
流畅的映射
扫描的源:
Fluent Mappings
Sources scanned:
DomainModel.NHibernate,Version = 1.0.0.0,Culture = neutral,
PublicKeyToken = null
Project.DomainModel.NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
发现的映射:
未找到
扫描的来源:
未找到
跳过的类型:
未找到
候选类型:
未找到
我读了很多文档和问题,看来我错过了一些很明显的东西:
I've read tons of documentation and questions, it seems I'm missing something really obvious :(
推荐答案
Fluently.Configure
支持所有不同的映射类型(通过代码,流利,自动映射,
All different mapping types (by code, fluent, automapping, xml...) are supported by Fluently.Configure
. It just needs different kind of code snippet to add it to your configuration.
这里有一些不同的方式通过流利地添加映射到你的配置 / code>配置
Here are some different ways to add mappings to your configuration via Fluently
configuration
.Mappings(m =>
{
foreach (var assembly in MapAssemblies)
{
m.FluentMappings.AddFromAssembly(assembly);
m.HbmMappings.AddFromAssembly(assembly);
m.AutoMappings.Add(..)
}
})
...
.ExposeConfiguration(cfg => {
foreach (var assembly in MapAssemblies)
{
cfg.AddDeserializedMapping(HbmMappingHelper.GetMappings(assembly), null);
cfg.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
System.Reflection.Assembly.GetExecutingAssembly()));
}
上面使用的助手类:
public class HbmMappingHelper
{
public static HbmMapping GetMappings(Type[] types)
{
ModelMapper mapper = new ModelMapper();
foreach (var type in types)
{
mapper.AddMappings(Assembly.GetAssembly(type).GetExportedTypes());
}
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
public static HbmMapping GetMappings(Assembly assembly)
{
ModelMapper mapper = new ModelMapper();
mapper.AddMappings(assembly.GetExportedTypes());
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}
}
这篇关于流畅的nhibernate配置不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!