gpt4 book ai didi

c# - 为什么 NHibernate 不从数据库中删除?

转载 作者:行者123 更新时间:2023-11-30 15:28:53 25 4
gpt4 key购买 nike

我遇到 NHibernate 没有从数据库中删除行的问题。 Nhibernate 正在毫无问题地保存和更新到同一个数据库。

运行 SQL 事件探查器后,似乎没有删除 SQL 被发送到数据库 - 这让我认为这是一个配置问题,但对我来说没有什么突出...

配置

Nhibernate 版本:3.3.1.4000FluentNHibernate 版本:1.3.0.733SQL 服务器版本:2008R2

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.connection_string_name">IntermediateDatabase</property>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="show_sql">true</property>
<property name="connection.release_mode">auto</property>
<property name="adonet.batch_size">500</property>

<!-- Mapping assemblies -->
<!-- Can't map it for Fluent NHibernate here; instead, load the mapping assembly in Global.asax.cs.
If you're still using HBMs, you can use the mapping here or pass the assembly via Global.asax.cs
as well, just like you can do with the Fluent NHibernate assembly(s). -->

</session-factory>
</hibernate-configuration>

谢谢

最佳答案

根据您评论的信息...看来您的工作方法是这样的:

更新

public T SaveOrUpdate(T entity) 
{
using (Session)
{
using (TransactionScope scope = new TransactionScope())
{
Session.SaveOrUpdate(entity); scope.Complete();
}
return entity;
}
}

这是绝对正确的...因为您的 session FlushMode 很可能是:

session.FlushMode = FlushMode.Commit;

请在此处查看更多详细信息:Nhibernate Flush works commit doesn't

删除

但是可怜的姐姐 Delete() 并没有完全支持强大的 Update()

public void Delete(T entity) 
{
using (Session)
{
this.Session.Delete(entity);
}
}

因此,即使对于 Delete(), session FlushMode 仍然 Hook 在 transaction 提交上......没有事务。这肯定(很有可能)是真正的原因(正如所怀疑的那样)

总结,将UpdateDelete 视为双胞胎......并给予他们相同的照顾 - 即交易

关于c# - 为什么 NHibernate 不从数据库中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24820127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com