ASP.Net MVC和NHibernate Burrow&Fluent NHibernate

时间:2022-10-06 03:10:39

I am getting the following runtime error in my ASP.Net MVC application:

我在ASP.Net MVC应用程序中收到以下运行时错误:

NHibernate.MappingException: No persister for: MyProject.Model.MyDomainObject

I am referencing the burrow and fluent binaries in my application and am reconfiguring burrow in Global.asax on Application_Start as follows:

我在我的应用程序中引用了洞穴和流畅的二进制文件,并在Application_Start上重新配置了Global.asax中的洞穴,如下所示:

var bf = new BurrowFramework();
IFrameworkEnvironment fe = bf.BurrowEnvironment;
Configuration cfg = fe.GetNHConfig("PersistenceUnit1");
cfg.AddMappingsFromAssembly(Assembly.LoadFrom(Server.MapPath("~/bin/MyProject.Data.dll")));
fe.RebuildSessionFactories();

I cannot for the life of me figure this out. If I setup a fluent NHibernate AutoPersistenceModel with my domain objects then everything works fine, it just doesn't work for manually configured fluent maps.

我不能为我的生活弄清楚这一点。如果我使用我的域对象设置流畅的NHibernate AutoPersistenceModel,那么一切正常,它只适用于手动配置的流畅地图。

The single map I have is as follows:

我的单一地图如下:

public class MyDomainObjectMap : ClassMap<MyDomainObject>
{
    public MyDomainObjectMap()
    {
        WithTable("my_domain_object");
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Description);
    }
}

Any help would be much appreciated, please let me know if you need more detail.

非常感谢任何帮助,如果您需要更多细节,请告诉我。

Thanks

2 个解决方案

#1


To answer why your initial approach failed, cfg.AddMappingsFromAssembly() scans the target assembly for preconfigured, embedded XML mapping files built into the assembly. Since you are generating the mappings 'Fluently' at runtime, these XML files do not exist.

为了解答初始方法失败的原因,cfg.AddMappingsFromAssembly()扫描目标程序集以查找程序集中内置的预配置嵌入式XML映射文件。由于您在运行时生成“Fluently”映射,因此这些XML文件不存在。

The following, on the other hand, reflects over the assembly to find for your defined 'FluentMappings' (i.e. those deriving from ClassMap<> ), generates the mapping dynamically, and injects it into the configuration. The mappings do not exist until you call into FluentMappings.AddFromAssembly()

另一方面,以下内容反映了程序集以查找已定义的“FluentMappings”(即从ClassMap <>派生的那些),动态生成映射,并将其注入到配置中。在调用FluentMappings.AddFromAssembly()之前,映射不存在

Fluently.Configure(cfg)
    .Mappings(m => m.FluentMappings.AddFromAssembly(assembly))

#2


Ok I have got it to work by doing the following:

好的,我通过执行以下操作让它工作:

var bf = new BurrowFramework();
IFrameworkEnvironment fe = bf.BurrowEnvironment;
Assembly assembly = Assembly.LoadFrom(Server.MapPath("~/bin/MyProject.Data.dll"));
Configuration cfg = fe.GetNHConfig("PersistenceUnit1");

Fluently.Configure(cfg)
    .Mappings(m => m.FluentMappings.AddFromAssembly(assembly))
    .BuildConfiguration();

fe.RebuildSessionFactories();

Does anyone know why my previous approach didn't work?

有谁知道为什么我以前的方法不起作用?

Thanks

#1


To answer why your initial approach failed, cfg.AddMappingsFromAssembly() scans the target assembly for preconfigured, embedded XML mapping files built into the assembly. Since you are generating the mappings 'Fluently' at runtime, these XML files do not exist.

为了解答初始方法失败的原因,cfg.AddMappingsFromAssembly()扫描目标程序集以查找程序集中内置的预配置嵌入式XML映射文件。由于您在运行时生成“Fluently”映射,因此这些XML文件不存在。

The following, on the other hand, reflects over the assembly to find for your defined 'FluentMappings' (i.e. those deriving from ClassMap<> ), generates the mapping dynamically, and injects it into the configuration. The mappings do not exist until you call into FluentMappings.AddFromAssembly()

另一方面,以下内容反映了程序集以查找已定义的“FluentMappings”(即从ClassMap <>派生的那些),动态生成映射,并将其注入到配置中。在调用FluentMappings.AddFromAssembly()之前,映射不存在

Fluently.Configure(cfg)
    .Mappings(m => m.FluentMappings.AddFromAssembly(assembly))

#2


Ok I have got it to work by doing the following:

好的,我通过执行以下操作让它工作:

var bf = new BurrowFramework();
IFrameworkEnvironment fe = bf.BurrowEnvironment;
Assembly assembly = Assembly.LoadFrom(Server.MapPath("~/bin/MyProject.Data.dll"));
Configuration cfg = fe.GetNHConfig("PersistenceUnit1");

Fluently.Configure(cfg)
    .Mappings(m => m.FluentMappings.AddFromAssembly(assembly))
    .BuildConfiguration();

fe.RebuildSessionFactories();

Does anyone know why my previous approach didn't work?

有谁知道为什么我以前的方法不起作用?

Thanks