nHibernate HQL - entity is not mapped

2019-04-18 22:37发布

I have my nHibernate setup and working correctly with QueryOver for most queries, however, whenever I try to do a HQL CreateQuery I get the exception that the entity isn't mapped. I can confirm that the same entity works fine using QueryOver.

Note: I am using fluent nHibernate

Any ideas what would cause this?

1条回答
Bombasti
2楼-- · 2019-04-18 23:03

If you have disabled auto-import in your mappings (<hibernate-mapping auto-import="false">), then you will have to use fully-qualified class names everywhere in your queries, unqualified class names won't work.

Otherwise, enable auto-import.

Conventions.Setup(x =>
                     {
                         x.Add(FluentNHibernate.Conventions.Helpers.AutoImport.Always()); // AutoImport.Never
                     }); // End FluentMappings.Conventions.Setup

Like this:

/*
var model = AutoMap.AssemblyOf<MyDb>()
                .Where(t => t.Namespace.StartsWith("MyDb.Tables"))
                .Conventions.AddFromAssemblyOf<MyDb>();
*/




        protected static AutoPersistenceModel CreateMappings()
        {
            //return new AutoPersistenceModel().AddMappingsFromAssemblyOf<MyDB.Tables.T_Admin>();

            return new AutoPersistenceModel().AddMappingsFromAssemblyOf<MyDb.Tables.T_Admin>()
                 .Where(t => t.Namespace == "MyDb.Tables");
        }

    private static ISessionFactory CreateMsSqlSessionFactory()
    {
        //AutoPersistenceModel model = CreateAutoMappings();
        AutoPersistenceModel model = CreateMappings();

        return Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2005
            .ConnectionString(c => c
                //.Server("MYCOMPUTER\\SQLEXPRESS")
                .Server("localhost")
                //.Database("testdb")
                .Database("nhDMS")
                .Username("TableCreatorWebServices")
                .Password(DB.Tools.Cryptography.AES.DeCrypt("AES_ENCRYPTED_PW"))))
            //.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SsoToken>()) 
            .Mappings(m => 
                {
                    m.AutoMappings.Add(model);
                    m.FluentMappings.Conventions.Setup(x =>
                     {
                         //x.AddFromAssemblyOf<MyDb.Tables.T_Admin>();
                         x.Add(FluentNHibernate.Conventions.Helpers.AutoImport.Always()); // AutoImport.Never
                     }); // End FluentMappings.Conventions.Setup
                }

            ) // End Mappings
            .ExposeConfiguration(BuildSchema)  // BuildSchema function call...
            .BuildSessionFactory();
    } // End Function CreateMsSqlSessionFactory
查看更多
登录 后发表回答