.net - 无法确定实体是暂时的还是分离的

标签 .net nhibernate transient

在尝试使用手动生成的 ID 持久化实体时,我在 NHibernate (v3.3) 中遇到了一个奇怪的错误:

Unable to determine if {Entity} with assigned identifier {Id} is transient or detached; querying the database. Use explicit Save() or Update() in session to prevent this.



但问题是,我使用的是 Save而不是 SaveOrUpdate .可能是什么问题呢?

最佳答案

事实证明,我的问题实际上是在保存父实体时发生的,其中包含一对多关系中的子实体:

<class xmlns="urn:nhibernate-mapping-2.2" name="ParentTable" table="ParentTable">

    <id name="ManuallyAssignedId">
      <generator class="assigned" />
    </id>

    <!- child table also has a manually assigned id -->
    <bag cascade="all" inverse="true" name="ChildTable">
      <key>
        <column name="ParentTable_id"/>
      </key>
      <one-to-many class="ChildTable" />
    </bag>

</class>  

换句话说,调用 Save对父实体造成 SaveOrUpdate关于 NHibernate 提示的子实体。

当我意识到这一点时,我很快找到了这个 StackOverflow 线程:How to save a child with assigned id in nhibernate ,其中有两个很好的建议:
  • Create and map a Version or Timestamp column - 如果它为空,NHibernate 将知道它需要持久化实体,或者
  • Attach a custom Interceptor to a session (or session factory)并使用自定义私有(private)字段来跟踪实体是否需要持久化。
  • 关于.net - 无法确定实体是暂时的还是分离的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12917217/

    相关文章:

    c# - 在 NHibernate 中为子查询使用接口(interface)

    c# - 如何在我的 NHibernate 查询中只选择几列?

    Grails域类 transient 集合属性 setter 问题

    ios - 需要帮助了解 IOS 核心数据中的 transient 属性

    c# - 通过 app.config 配置线程池最大线程数?

    c# - 如何排序 List<T> 其中 T 是我自己的类,我想按 class.someString 排序

    nhibernate - NHibernate ISession用法和线程安全

    c# - 为什么在嵌套容器上将 Structuremap 的 Transient 生命周期视为 ContainerScoped?

    c# - 在没有 COM 的 Linux 上通过 PHP 运行 .NET DLL

    c# - Trace.WriteLine 在使用 CSharpCodeProvider 编译的动态加载程序集中不起作用