NHibernate警告,并且数据未保存 [英] NHibernate WARNING and the data is not saved
问题描述
虽然我通过调用SaveOrUpdate()进行保存,但收到此警告,并且在调用Transaction.Commit()之后数据没有保存在数据库中.
While I am saving by calling SaveOrUpdate(), I got this warning and the data is not saving in the database after calling Transaction.Commit().
NHibernate.Engine.ForeignKeys-无法 确定[项目名称]是否与 分配的标识符[primarykey]为 暂时的或分离的;查询 数据库.使用显式的Save()或 会话中的Update()可以防止这种情况.
NHibernate.Engine.ForeignKeys - Unable to determine if [project name] with assigned identifier [primarykey] is transient or detached; querying the database. Use explicit Save() or Update() in session to prevent this.
我正在插入一个新对象. Google搜索告诉我调用Save()而不是SaveOrUpdate():这意味着Save()仅用于插入.
I am inserting a new object. Google search tell me to call Save() instead of SaveOrUpdate(): means Save() is only for inserting.
我在Google中搜索,对此了解不多. 有人可以给我关于这个问题或警告的建议吗?
I search in Google and do not see much about this. Could anyone give me suggestion for this problem or this warning?
这是模拟的样本映射文件-
Here is the simulated sample mapping files -
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly=""
namespace="">
<class name="Customer" table="[dbo].[Customer]" optimistic-lock="none" >
<id name="CustomerId" column="CustomerId" >
<generator class="assigned"/>
</id>
<property name="Name" column="Name" />
<property name="Age" column="Age" />
<set name="CustomerDetails" cascade="none" inverse="true" fetch="select">
<key>
<column name="CustomerId"/>
</key>
<one-to-many class="CustomerDetail"/>
</set>
<many-to-one name="MGender" fetch="select" cascade="none">
<column name="GenderCode"/>
</many-to-one>
</class>
</hibernate-mapping>
<class name="CustomerDetails" table="[dbo].[CustomerDetail]" optimistic-lock="none" >
<id name="CustomerDetailId" column="CustomerDetailId" >
<generator class="assigned"/>
</id>
<property name="Detail1" column="Detail1" />
<many-to-one name="Customer" fetch="select" cascade="none">
<column name="CustomerId"/>
</many-to-one>
</class>
<class name="MGender" table="[dbo].[MGender]" optimistic-lock="none" >
<id name="GenderCode" column="GenderCode" >
<generator class="assigned"/>
</id>
<property name="Description" column="Description" />
<set name="Customers" cascade="none" inverse="true" fetch="select">
<key>
<column name="GenderCode"/>
</key>
<one-to-many class="Customer"/>
</set>
</class>
推荐答案
您正在使用分配的标识符,因此需要设置未保存的值属性,以便NHibernate可以确定是否应插入或更新实体.或者,您可以显式调用保存"以获取新实体.
You're using an assigned identifier so you need to set the unsaved-value attribute so that NHibernate can determine if an entity should be inserted or updated. Or you can explicitly call Save for new entities.
<id name="CustomerId" column="CustomerId" unsaved-value="???" >
<generator class="assigned"/>
</id>
这篇关于NHibernate警告,并且数据未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!