NHibernate:使用数据库中只与FK相关的未提交数据填充列表 [英] NHibernate: populate list with uncommitted data related only by FK in database
问题描述
这是这个问题的阐述和澄清。
假设我有两个表, Foo
和 Bar
。
$ b Bar
的FK为 Foo
。
在应用程序中,表由类表示, Foo
有一个酒吧
秒。
Bar 具有
,我在数据库中添加一个 Foo
的id属性,它在数据库中有一个FK。 / p>
$ b 在会话
和交易
的上下文中, code> IsolationLevel.ReadUncommitted Foo
的实例,将生成的id赋给 Foo_id
Bar
的一个实例的属性,并将其添加到数据库。
Transaction.Commit()之前, 有可能让NHibernate读出 Foo
与数据库中的 Bar
列表?也就是说,读取的数据未提交?
我有创建了一个演示这个的VS2012项目。它包括一个SSDT项目,用于构建所需的数据库,并且有测试显示我询问的内容。
谢谢
但是,NHibernate通常不会更新已经加载的对象。但是如果你Flush(),接着是Clear()或者Evict(),然后再读取Foo,它的集合将包含Bar的实例。 (只要你在同一个会话中,提交事务与此无关,除了使用默认设置Commit()自动调用Flush()。)
This is an elaboration and clarification of this question.
Suppose I have two tables, Foo
and Bar
.
Bar
has a FK to Foo
.
In the application, the tables are represented by classes and Foo
has a list of Bar
s.
Bar
has a property for the id of the Foo
it has a FK to in the database.
In the context of a Session
and Transaction
with IsolationLevel.ReadUncommitted
, I add an instance of Foo
to the database, assign the generated id to the Foo_id
property of an instance of Bar
and also add it to the database.
Now, before calling Transaction.Commit()
, is it possible to have NHibernate read out Foo
with a list of Bar
from the database? That is, read the data uncommitted?
I have created a VS2012 project which demonstrates this. It includes a SSDT project for building the required database and has tests showing what I asking about.
Thank you.
In the context of the same transaction you can read back all changes you've made to the database, so yes. The isolation level of the transaction doesn't matter for changes made within that transaction.
However, NHibernate typically won't update an already loaded object. But if you Flush(), followed by either Clear() or Evict() and then read Foo again, its collection will include the instance of Bar. (As long as you are in the same session, committing the transaction is irrelevant to this, except that with default settings Commit() automatically calls Flush().)
这篇关于NHibernate:使用数据库中只与FK相关的未提交数据填充列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!