Fluent mapping - entities and classmaps in different assemblies

坚强是说给别人听的谎言 提交于 2019-12-04 12:44:06

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.

Is it a problem that my Entities and my ClassMaps are in different assemblies?

No there is nothing wrong with that as long as you ClassMap project have refrerence to your Entities project

anyway try this :

m.FluentMappings.AddFromAssemblyOf<UserMapping>()

if this doesn't work post the entire error

Certainly having your entities in a different assembly should not cause a problem as Yassir alludes to above.

According to the Fluent NHibernate Wiki the AddFromAssemblyOf method infers or reflects on the Assembly that contains all of your entities and will map to them when you supply any entity name to it. From the documentation on the FNH wiki you would construct the method as follows:

 m.FluentMappings
  .AddFromAssemblyOf<YourEntity>(); 

Therefore in your example, if the entity you are mapping is named User then your code should be constructed as follows:

 m.FluentMappings
  .AddFromAssemblyOf<User>();

Hope this is of help.

dbones

has this been solved? if not could you inlcude your setup?

for example here is my example one

public static ISessionFactory GetSessionFactory()
    {
        //Old way, uses HBM files only
        //return (new Configuration()).Configure().BuildSessionFactory(); //requies the XMl file.

        //get database settings.
        Configuration cfg = new Configuration();//.Configure();

        ft = Fluently.Configure(cfg);

        //DbConnection by fluent
        ft.Database
            (
            MsSqlConfiguration
                .MsSql2005
                .ConnectionString(c => c
                   .Server(".\\SqlExpress")
                   .Database("NhibTest")
                   .TrustedConnection()
                )
                .ShowSql()
                .UseReflectionOptimizer()
            );

        //set up the proxy engine
        //cfg.Properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");

        //get mapping files.
        ft.Mappings(m =>
             {
                 //set up the mapping locations
                 m.FluentMappings.AddFromAssemblyOf<PersonMap>();//.ExportTo("C:\\mappingfiles");
                 //m.Apply(cfg);
             });

        //return the SessionFactory
        return ft.BuildSessionFactory();
    }

the project structure is as follows

  • project.Data <- mapping files, and Dao's (also hibernate session manager, containing the above code)
  • project.Core <- POCO's
  • project.UI

also have look here incase you have a mixture of Fluent NHibernate and NHibernate configuration

Finally have a look at S#arp Architectures way, as i think it includes this mixture

NhibernateSession <- function : private static ISessionFactory CreateSessionFactoryFor

Hope this helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!