简体   繁体   English

NHibernate GenericADOException

[英]NHibernate GenericADOException

sesion.Get(id); session.Get(id); produces sometimes a GenericADOException:有时会产生 GenericADOException:

could not load an entity: [PFC__ Servidor.Entidades.Usuario#10][SQL: SELECT usuario0 .us_id as us1_1_0_, usuario0_.us_email as us2_1_0_, usuario0_.us_contrasena as us3_1_0_ FROM public.usuario usuario0_ WHERE usuario0_.us_id=?]无法加载实体:[PFC__ Servidor.Entidades.Usuario #10][SQL: SELECT usuario0 .us_id as us1_1_0_, usuario0_.us_email as us2_1_0_, usuario0_.us_contrasena as us3_1_0_ FROM usuario_ usuario0_?]

Any known where is the problem?任何已知的问题出在哪里? Why not always fails?为什么不总是失败?

PD: Sorry for my english. PD:对不起我的英语。

ACT1: Inner exception: [System.NullReferenceException] "Object reference not set to an instance of an object" ACT1:内部异常:[System.NullReferenceException]“对象引用未设置为对象的实例”

Can you post the innerException?你可以发布innerException吗?

  • Are you sure your connectionstring is wellformed?您确定您的连接字符串格式正确吗?
  • Are you sure your database has been created?你确定你的数据库已经创建了吗?
  • Are you sure your mapping file point at right table name?你确定你的映射文件指向正确的表名吗?
  • Are you sure your database instance is running?您确定您的数据库实例正在运行吗?

update更新

I also found this:我还发现了这个:

Could not load an entity when table is existing表存在时无法加载实体

sometimes有时

Is your session shared among threads?您的 session 是否在线程之间共享? If so, death.如果是这样,死亡。 Sessions are not thread safe.会话不是线程安全的。

I had this issue when I started implementing the current framework.当我开始实施当前框架时,我遇到了这个问题。 It is in fact as Jason is suggesting - you need to set your NHibernate session scope to per-request.事实上,正如 Jason 所建议的那样 - 您需要将 NHibernate session scope 设置为每个请求。 This means a new session will be opened for each request and closed at end of request.这意味着将为每个请求打开一个新的 session 并在请求结束时关闭。 However, you only create the session factory once per application start since this is the most expensive part.但是,您只需在每个应用程序启动时创建一次 session 工厂,因为这是最昂贵的部分。

Are you using any dependency injection?您是否使用任何依赖注入? You can find great examples on how to achieve per-request session scoping on Windsor Castle NHibernate facility .您可以在温莎城堡 NHibernate 设施上找到有关如何实现按请求 session 范围界定的很好的示例。

If you are doing it manually however, you can open an NHibernate session in your Global.asax Request_begin and ensure it is closed and disposed in Request_End method.但是,如果您手动执行此操作,则可以在 Global.asax Request_begin 中打开 NHibernate session 并确保它在 Request_End 方法中关闭和处置。

A neat way to work with NHIbernate is:使用 NHIbernate 的一种简洁方法是:

  • Castle Windsor (dependency injection)温莎城堡(依赖注入)
  • Repository pattern (separate your data access)存储库模式(分离您的数据访问)
  • Unit of work pattern.工作单元模式。 Instead of injecting nhibernate session you inject correct repository and unit of work into services而不是注入 nhibernate session 您将正确的存储库和工作单元注入服务
  • Service layer (business logic) - what you call from your controllers or application presentation layer服务层(业务逻辑) - 您从控制器或应用程序表示层调用的内容

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

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