错误:在不同的程序集中引用映射的流利的NHibernate映射 [英] Error: fluent NHibernate mapping which references mapping in different assembly

查看:237
本文介绍了错误:在不同的程序集中引用映射的流利的NHibernate映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司有多个参考相同的DB和核心代码库的网站。
然后我们有一个CMS来管理这些数据。

在Core库中,我有一个Site类,它包含了关于每个站点的一些基本信息。这是我的流利映射。

 使用Core.Model; // Site类存在的位置

名称空间Core.Repository.NHibernate.Mappings
{
public class SiteMapping:ClassMap< Site>
{
public SiteMapping()
{
Table(f_site);
Id(x => x.Id,f_site_id)。GeneratedBy.Identity();
Map(x => x.Domain,domain_name)。Not.Nullable();


$ b code
$ b

作为CMS的一部分,我们记录谁编辑什么和什么时候。但我只想在CMS中引用Log类和映射,而不是在核心代码中,因为人们只能通过CMS编辑信息。



这是我目前流利的映射到引用Site类的日志类。

 使用Core.Model; // Site类型存在
使用Cms.Model; // Log和CmsUser类存在

namespace Cms.Repository.NHibernate.Mappings
{
public class LogMapping:ClassMap< Log>
{
public LogMapping()
{
Table(f_log);
Id(x => x.Id,f_log_id)。GeneratedBy.Identity();
Map(x => x.Message,message);
Map(x => x.LogType,d_log_type_id)。CustomType< LogType>();
Map(x => x.LogOperation,d_log_operation_id)。CustomType< LogOperation>();
Map(x => x.Date,log_date);

参考< Site>(x => x.Site,f_site_id)
.ForeignKey(f_site_id)
.Cascade.None();

参考< CmsUser>(x => x.User,userId)
.ForeignKey(userId)
.Cascade.None();





$ b理论上这个工作很好,但用下面的日志映射错误:

  Cms.Tests.Repository.NHibernate.Repository.TestLogRepository.TestLogMappings:
StructureMapException:StructureMap异常代码:207
创建PluginType实例e46153a3-2bfe-4279-8749-a42d7a6dd10c时发生内部异常Core.Repository.NHibernate.SessionStorage.ISessionContainer`1 [[HbmCms.Repository.NHibernate。 Mappings.Config.LogMapping,Cms.Repository.NHibernate,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]],Core.Repository.NHibernate,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null。检查内部异常的更多细节。
----> StructureMapException:StructureMap异常代码:207
创建实例'9e72c2ff-e3f4-4b54-9f34-3422a7b982a7'的PluginType Core.Repository.NHibernate.SessionStorage.ISessionFactoryContainer`1 [[Cms.Repository.NHibernate。 Mappings.Config.LogMapping,Cms.Repository.NHibernate,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]。检查内部异常的更多细节。
----> FluentNHibernate.Cfg.FluentConfigurationException:创建SessionFactory时使用了无效或不完整的配置。检查PotentialReasons集合和InnerException获取更多细节。


----> NHibernate.MappingException:来自表f_log的关联指的是一个未映射的类:Core.Model.Site

有没有人有任何想法如何让我的CMS映射引用核心站点映射?这是在两个项目中获得映射的第一个代码,但是我们将做一些相当多的事情,就像你在CMS中看到和做的很多事情一样。如果我可以避免它,我真的不想把CMS唯一的代码放到核心库。



感谢您的帮助



Sandra

解决方案

未映射的类错误通常源于Configuration没有记录该ClassMap。 / p>

在您的 FluentConfiguration 中,您可能有类似的内容:

  .Mappings(m => m.FluentMappings 
.AddFromAssemblyOf< T>())

看来你的两个ClassMaps(至少在后面提到的那些)在不同的程序集中。您可以指定多个程序集:

  .Mappings(m => m.FluentMappings 
.AddFromAssemblyOf< T1> ()
.AddFromAssemblyOf< T2>())

,您可以添加

  .Diagnostics(d => d.Enable()。OutputToConsole())

添加到您的 FluentConfiguration 中,它将返回类映射,约定适用于他们,为什么/为什么他们是有效的。


My company has multiple sites which reference the same DB and Core library of code. Then we have a CMS which manages the data.

In the Core library I have a Site class which holds a bunch of basic info about each site. This is the fluent mapping I have for it.

using Core.Model;  // Where Site class exists

namespace Core.Repository.NHibernate.Mappings
{
    public class SiteMapping : ClassMap<Site>
    {
        public SiteMapping()
        {
            Table("f_site");
            Id(x => x.Id, "f_site_id").GeneratedBy.Identity();
            Map(x => x.Domain, "domain_name").Not.Nullable();
        }
    }
}

As part of the CMS, we keep a log of who edited what and when. But I want to only have a reference to the Log class and mapping in the CMS, rather than in my core code, as people can only edit info via the CMS.

Here is my current fluent mapping to the Log class, which referneces the Site class.

using Core.Model; // where Site class exists
using Cms.Model; // where Log and CmsUser classes exists

namespace Cms.Repository.NHibernate.Mappings
{
    public class LogMapping : ClassMap<Log>
    {
        public LogMapping()
        {
            Table("f_log");
            Id(x => x.Id, "f_log_id").GeneratedBy.Identity();
            Map(x => x.Message, "message");
            Map(x => x.LogType, "d_log_type_id").CustomType<LogType>();
            Map(x => x.LogOperation, "d_log_operation_id").CustomType<LogOperation>();
            Map(x => x.Date, "log_date");

            References<Site>(x => x.Site, "f_site_id")
                .ForeignKey("f_site_id")
                .Cascade.None();

            References<CmsUser>(x => x.User, "userId")
                .ForeignKey("userId")
                .Cascade.None();
        }
    }
}

In theory this works great, but the Log mapping errors with the following

Cms.Tests.Repository.NHibernate.Repository.TestLogRepository.TestLogMappings:
StructureMap.StructureMapException : StructureMap Exception Code:  207
Internal exception while creating Instance 'e46153a3-2bfe-4279-8749-a42d7a6dd10c' of PluginType Core.Repository.NHibernate.SessionStorage.ISessionContainer`1[[HbmCms.Repository.NHibernate.Mappings.Config.LogMapping, Cms.Repository.NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Core.Repository.NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.  Check the inner exception for more details.
  ----> StructureMap.StructureMapException : StructureMap Exception Code:  207
Internal exception while creating Instance '9e72c2ff-e3f4-4b54-9f34-3422a7b982a7' of PluginType Core.Repository.NHibernate.SessionStorage.ISessionFactoryContainer`1[[Cms.Repository.NHibernate.Mappings.Config.LogMapping, Cms.Repository.NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].  Check the inner exception for more details.
  ----> FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.


  ----> NHibernate.MappingException : An association from the table f_log refers to an unmapped class: Core.Model.Site

Does anyone have any idea how to get my CMS mapping to reference the Core Site mapping? This is the first bit of code that is getting a mapping across two projects, but its something that we'll be doing a fair bit of, as lots of stuff you only look at and do in the CMS. I don't really want to put the CMS only code into the Core library if i can avoid it.

Thanks for the help

Sandra

解决方案

An 'unmapped class' error usually stems from the Configuration not having that ClassMap recorded.

In your FluentConfiguration, you probably have something similar to:

.Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<T>())

It appears that your two ClassMaps (at least those mentioned in the post) are in different assemblies. You can specify multiple assemblies with:

.Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<T1>()
                .AddFromAssemblyOf<T2>())

To see exactly what is being mapped, you can add

.Diagnostics(d => d.Enable().OutputToConsole())

to your FluentConfiguration and it will return the class maps, the conventions applied to them and why/why not they were valid.

这篇关于错误:在不同的程序集中引用映射的流利的NHibernate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆