简体   繁体   English

流利的NHibernate无法使用自动映射执行查询

[英]Fluent NHibernate not able to execute query with automapping used

I've been beating my head on a wall for the last few days as to why I get this odd behavior 最近几天,我一直在脑海中犹豫不决,为什么会出现这种奇怪的行为

I have tried the 2 following setups 我尝试了以下2种设置

1: 1:

return _sessionFactory ?? 
    (_sessionFactory = Fluently.Configure()
        .Database(MySQLConfiguration.Standard.ConnectionString(cs =>cs.Server("127.0.0.1").Database("azeroth").Username("root").Password("password")))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Database>())
        .BuildSessionFactory()
    );

2: 2:

return _sessionFactory ?? 
    (_sessionFactory = Fluently.Configure()
        .Database(MySQLConfiguration.Standard.ConnectionString(cs => cs.Server("127.0.0.1").Database("azeroth").Username("root").Password("password")))
        .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Database>().Where(t => t.Namespace.StartsWith("Azeroth.Database.Auth"))))
        .BuildSessionFactory()
    );

I have verified that my classes used for the database have a namespace that starts with Azeroth.Database.Auth 我已验证用于数据库的类具有以Azeroth.Database.Auth开头的命名空间

If I use #1 everything works pefectly I can query/add/delete/update the database. 如果我使用#1,一切都会正常进行,我可以查询/添加/删除/更新数据库。

If I use #2 I get an exception - unable to execute query - on EVERY database operation. 如果我使用#2,则会在每次数据库操作时遇到异常-无法执行查询。

Hoping someone can shed some light as to why the automapping line doesn't work but the mapping line works. 希望有人可以阐明为什么自动映射线不起作用而映射线起作用的原因。

This is the output of the exception: 这是异常的输出:

NHibernate.Exceptions.GenericADOException: could not execute query at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() at NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd) at NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session) at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplement NHibernate.Exceptions.GenericADOException:无法在MySql.Data.MySqlClient.Driver.NextResult(MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&受影响的行,Int64&insertId)处的MySql.Data.MySqlClient.MySqlStream.ReadPacket()处执行查询Int32 statementId,布尔值强制),位于MySql.Data.MySqlClient。 NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)上的.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()在NHibernate.Loader.Loader.GetResultSet(IDbCommand st,布尔型autoDiscoverTypes,布尔型可调用,RowSelection选择,ISessionImplementor会话)在NHibernate.Loader.Loader.DoQuery(ISessionImplementor会话,QueryParameters queryParameters,Boolean returnProxies)在NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplement) or session, QueryParameters queryParameters, Boolean returnProxies) at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) --- End of inner exception stack trace --- 或会话,位于NHibernate.Loader.Loader.DoList(ISessionImplementor会话,QueryParameters queryParameters)上的QueryParameters queryParameters,布尔型returnProxies)-内部异常堆栈跟踪的结尾-

[ SELECT this_.Id as Id0_0_, this_.Username as Username0_0_, this_.Sha_Pass_hash as Sha3_0_0_, this_.Email as Email0_0_, this_.Joindate as Joindate0_0_, this_.LaspIp as LaspIp0_0_, this_.FailedLogins as FailedLo7_0_0_, this_.Locked as Locked0_0_, this_.LastLogin as LastLogin0_0_, this_.Online as Online0_0_, this_.Expansion as Expansion0_0_, this_.Mutetime as Mutetime0_0_, this_.Mutereason as Mutereason0_0_, this_.Muteby as Muteby0_0_, this_.Locale as Locale0_0_, this_.Os as Os0_0_, this_.Recruiter as Recruiter0_0_, this_.Salt as Salt0_0_ FROM Account this_ WHERE this_.Username = ?p0 ] [选择this_.Id作为Id0_0_,this_.Username作为Username0_0_,this_.Sha_Pass_hash作为Sha3_0_0_,this_.Email作为Email0_0_,this_.Joindate作为Joindate0_0_,this_.LaspIp作为LaspIp0_0_,this_.Failed_Log,将其锁定为FailedLo7。 this_.LastLogin作为LastLogin0_0_,this_.Online作为Online0_0_,this_.Expansion作为Expansion0_0_,this_.Mutetime作为Mutetime0_0_,this_.Mutereason作为Mutereason0_0_,this_.Muteby作为Muteby0_0_,this_.Locale作为Locale0_0_,招聘者为Recruiter0_0_,此_盐为Salt0_0_ FROM Account this_ WHERE this_.Username =?p0]

So I can see with automapping its mapping the class to the database. 因此,可以通过自动映射将类映射到数据库。

I'm wanting to use the Automapping because I'm working with 3 different databases on 1 single server depending on where the data is so I have the classes in different namespaces hoping to pull 3 different sessions factories based on namespace as in the second one. 我想使用自动映射,因为我正在1个服务器上使用3个不同的数据库,具体取决于数据的位置,因此我在不同名称空间中的类希望根据名称空间提取3个不同的会话工厂,如第二个中所示。

Because one time you add FluentNhibernate Mappings to the factory and the other time you try to add AutomapperMappings to the factory. 因为一次您将FluentNhibernate映射添加到工厂,而另一次您尝试将AutomapperMappings添加到工厂。

Both are completely different mapping strategies. 两者都是完全不同的映射策略。 I expect that for the fluent mappings you have all your classes mapped with ClassMap<T> etc... 我希望对于流利的映射,您可以使用ClassMap<T>等映射所有类。

Automapper will not use your manually define Fluent Mappings , it will try to generate the mappings automatically based on your entities and some other settings and defaults. Automapper不会使用您手动定义的Fluent映射 ,它将尝试根据您的实体以及其他一些设置和默认值自动生成映射。

The outcome can of cause something completely different... Therefore it might be that your queries do not work anymore with one or the other... 结果可能会导致完全不同的结果。因此,可能是您的查询不再与另一个查询一起工作了。

So for example your entity is Account and with fluent nhibernate you mapped it to a table with a completely different name, eg MyAccounTable , how should Automapper know about that, it will simply do the mapping assuming the table name is equal to the entity name... Same for everything else... 因此,例如,您的实体是Account并且使用流畅的nhibernate将其映射到具有完全不同名称的表(例如MyAccounTable ,Automapper应该如何知道这一点,它会简单地进行映射,前提是表名等于实体名称。 ..其他都一样...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM