流利的映射 - 不同程序集中的实体和类映射 [英] Fluent mapping - entities and classmaps in different assemblies

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

问题描述

使用流畅的配置来指定流畅的映射如下:

$ p $ .Mappings(m => m.FluentMappings.AddFromAssembly (typeof(UserMapping).Assembly))

目前我得到一个NHibernate.MappingException:没有persister为错误。

这是一个问题,我的实体和我的ClassMaps在不同的程序集?据推测,AddFromAssembly对装有类映射的程序集感兴趣,而不是实体? (这是我所假设的)

谢谢!
$ b

更新:



对不起,没有回应的答案很快 - 我必须在设置赏金后出乎意料地旅行。



无论如何,谢谢你的回应。我已经看了他们,并更新我的代码使用AddFromAssemblyOf而不是AddFromAssembly,但仍得到相同的错误。可能我正在做一些愚蠢的事情。以下是我使用的会话工厂代码的完整代码:

  public class NHibernateHelper 
{
私人静态ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if(_sessionFactory == null)
{
var mysqlConfig = FluentNHibernate.Cfg。 Db.MySQLConfiguration
.Standard
.ConnectionString(CONNECTION STRING OMITTED)
.UseOuterJoin()
.ProxyFactoryFactory(NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode。城堡);
$ b _sessionFactory = FluentNHibernate.Cfg.Fluently.Configure()
.Database(mysqlConfig)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf< User>())
.BuildSessionFactory();


}

return _sessionFactory;


$ b $ public static ISession OpenSession()
{
return SessionFactory.OpenSession();


$ / code $ / pre

我试图运行一个测试nunit使用这个会话机制来使用一个仓库:

NHibernate.MappingException:没有persister for:xxxx.Model.Entities.User



谢谢

PS:
我尝试过使用AddFromAssemblyOf();
具有映射定义的项目(DataAccess)引用了实体(Model)的项目。

解决方案

什么版本的Fluent NHibernate你用?发行候选版本和1.0发行版本一直存在问题。您可能要考虑从SVN存储库下载最新版本。



http://fluent-nhibernate.googlecode.com/svn/trunk/

另外,您可能要检查连接字符串以确保它是完全正确的,并且您要确保下面的用户指向一个类。

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

另外,我应该提到当你使用AddFromAssemblyOf时,流利会尝试在该程序集中映射EVERY类。如果您在该名称空间中有其他任何类,则需要对其进行过滤。有几种不同的方法来完成这一点。最简单的方法是将所有想要映射的POCO放在自己的命名空间中,然后执行下面的操作。

  .Mappings(m => m.AutoMappings 
.Add(AutoMap.AssemblyOf< MyNamespace.Entities.MyClass>()
.Where(type => type.Namespace ==MyNamespace.Entities )

Where子句将过滤您不想映射的项目。 $ b

When using fluent configuration to specify fluent mappings like this:

.Mappings(m => m.FluentMappings.AddFromAssembly(typeof(UserMapping).Assembly))

At the moment I am getting a "NHibernate.MappingException : No persister for" error.

Is it a problem that my Entities and my ClassMaps are in different assemblies? Presumably AddFromAssembly is interested in the assembly that holds the class maps, not the entities? (that is what I have assumed)

Thanks!

UPDATE:

Sorry for not responding to answers very quickly - I had to travel unexpectedly after setting the bounty.

Anyway, thanks for the responses. I've taken a look through them and have updated my code to use AddFromAssemblyOf rather than AddFromAssembly, but still am getting the same error. Possibly I am doing something stupid. Here is the full code for the session factory code I am using:

public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;
        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    var mysqlConfig = FluentNHibernate.Cfg.Db.MySQLConfiguration
                        .Standard
                        .ConnectionString("CONNECTION STRING OMITTED")
                        .UseOuterJoin()
                        .ProxyFactoryFactory("NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");

                    _sessionFactory = FluentNHibernate.Cfg.Fluently.Configure()
                                        .Database(mysqlConfig)
                                        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<User>())
                                        .BuildSessionFactory();


                }

                return _sessionFactory;
            }
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }

I receive this exception when trying to run a test in nunit that makes use of a repository using this session mechanism:

NHibernate.MappingException : No persister for: xxxx.Model.Entities.User

Thanks

P.S.: I've tried using both and in AddFromAssemblyOf(); Project with mapping definitions (DataAccess) has reference to project with entities (Model).

解决方案

What version of Fluent NHibernate are you using? There have been problems with the release candidate and the 1.0 release versions. You may want to consider downloading the latest version from the SVN repository.

http://fluent-nhibernate.googlecode.com/svn/trunk/

Additionally, you may want to check the connection string to make sure that it is completely correct, and you want to make sure that "User" below points to a class.

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

Also, I should mention that when you use AddFromAssemblyOf, fluent will try to map EVERY class in that assembly. If you have any other classes in that namespace you will want to filter them. There are several different ways to accomplish this. The simplest way is to just place all of the POCOs you want to map in their own namespace and then do something like the following.

.Mappings(m => m.AutoMappings
                .Add(AutoMap.AssemblyOf<MyNamespace.Entities.MyClass>()
                .Where(type => type.Namespace == "MyNamespace.Entities")

The Where clause will filter items you don't want mapped.

这篇关于流利的映射 - 不同程序集中的实体和类映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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