Nhibernate LINQ

编程入门 行业动态 更新时间:2024-10-24 22:29:03
本文介绍了Nhibernate LINQ-缓存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码

factory = new Configuration().Configure().BuildSessionFactory(); session = factory.OpenSession(); var query = session.Linq<Root>(); var data = query.ToList<Root>(); var data2 = query.ToList<Root>();

这个生成2条SQL查询,所以我知道一级缓存不起作用. LINQ for nhibernate是否管理一级缓存?如果是,该如何配置?

This one generate 2 SQL queries, so i understand that first level cache isn't working. Does LINQ for nhibernate manage first level cache ? If yes, how to configure it ?

先谢谢了.

推荐答案

一个误解是,一级缓存通常避免执行sql.

It is a misconception that the first level cache avoids execution of sql in general.

查询始终在数据库上执行.这样可以100%确保结果完全相同,无论实例是否已经在内存中.例如,在使用联接或条件时,这可能很重要.如果查询实际上包含联接或where子句,则没有区别.

Queries are always executed on the database. This makes 100% sure that the result is exactly the same regardless if the instances are already in memory or not. This could be important for instance when using joins or conditions. There is no difference if the query actually contains joins or a where clause.

如果一级缓存代表数据库中的同一实例,请确保返回相同的实例. NHibernate执行sql,当它在内存中找到与查询返回的对象相同的对象时,则在缓存中返回该对象.但是在执行sql之前是不知道的.

The first level cache make sure that the same instance is returned if it represents the same instance in the database. NHibernate executes the sql, when it finds the same object in memory as returned by the query, then it returns the one in the cache. But it can't know before executing the sql.

仅当使用session.Get或session.Load时,NH才会进行缓存查找,并在找到实例时避免查询.

Only when you use session.Get or session.Load, NH does a cache lookup and avoids a query when it finds the instance.

AFAIK,适用于NHibernate的Linq在缓存方面没有什么特别之处.

AFAIK, Linq for NHibernate does nothing special with the cache.

更多推荐

Nhibernate LINQ

本文发布于:2023-11-16 21:59:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Nhibernate   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!