C# NHibernate:脱水属性错误 - 这是什么鬼?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10867390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NHibernate: Error dehydrating property - What the heck is this?
提问by Jeremy Holovacs
I'm doing a fairly complex NHibernate transaction in a financial system, creating a payment, recording the ledger entries, checking to see if the payment is the total amount of an invoice, if so marking the invoice as paid in full, etc... lots of fun stuff. Naturally it has to happen inside a single transaction.
我正在金融系统中进行相当复杂的 NHibernate 交易,创建付款,记录分类帐条目,检查付款是否是发票的总金额,如果是,则将发票标记为全额付款等。 . 很多有趣的东西。当然,它必须发生在单个事务中。
When I try to commit the change to the session, I get the following error:
当我尝试将更改提交到会话时,出现以下错误:
Error dehydrating property value for C3.DataModel.CFAPTransaction.Vendor
Googling this did not turn up many record. Can someone tell me what this means and where I need to focus my debugging efforts?
谷歌搜索这并没有出现很多记录。有人能告诉我这意味着什么以及我需要将调试工作集中在哪里吗?
UPDATE
更新
Per request, here is the full error message:
每个请求,这里是完整的错误消息:
NHibernate.PropertyValueException: Error dehydrating property v alue for C3.DataModel.CFAPTransaction.Vendor --->NHibernate.HibernateException: Unable to resolve property: APVendorId at NHibernate.Tuple.Entity.EntityMetamodel.GetPropertyIndex(String propertyName) at NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetPropertyValue(Object entity, String propertyPath) at NHibernate.Persister.Entity.AbstractEntityPersister.GetPropertyValue(Object obj, String propertyName, EntityMode entityMode) at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session) at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) --- End of inner exception stack trace --- at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityInsertAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) at NHibernate.Engine.ActionQueue.ExecuteActions() at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) at NHibernate.Impl.SessionImpl.Flush() at NHibernate.Transaction.AdoTransaction.Commit() at C3.DataModel.Repositories.NHUnitOfWork.Save() in C:\projects\C3\C3.DataModel.Generated\Generated\NHibernateRepositories.generated.cs:line 2659 at C3.WebUI.Areas.Finance.Controllers.AccountsPayableController.CreatePayment(CreatePaymentModel model) in C:\projects\C3\C3.WebUI\Areas\Finance\Controllers\AccountsPayableController.cs:line 434
NHibernate.PropertyValueException: Error dehydrating property v alue for C3.DataModel.CFAPTransaction.Vendor --->NHibernate.HibernateException:无法解析属性:APVendorId at NHibernate.Tuple.Entity.EntityMetamodel.GetPropertyIndex(String propertyName) at NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetPropertyValue(Object entity, String propertyPath) at NHibernate.Persister.Entity.AbstractEntity GetPropertyValue(Object obj, String propertyName, EntityMode entityMode) at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session) at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) 在 NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand 语句, ISessionImplementor session,Int32 index) --- 内部异常堆栈跟踪结束 --- 在 NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32表、IDbCommand 语句、ISessionImplementor 会话、Int32 索引)在 NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) 在 NHibernate。 Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityInsertAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Engine.ActionQueue .ExecuteActions(IList list) 在 NHibernate.Engine.ActionQueue。ExecuteActions() 在 NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) 在 NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) 在 NHibernate.Impl.SessionImpl.Flush() 在 NHibernate.Transaction.AdoTransaction.Commit ) 在 C3.DataModel.Repositories.NHUnitOfWork.Save() 在 C:\projects\C3\C3.DataModel.Generated\Generated\NHibernateRepositories.generated.cs:line 2659 在 C3.WebUI.Areas.Finance.Controllers.AccountsPayableController。 C:\projects\C3\C3.WebUI\Areas\Finance\Controllers\AccountsPayableController.cs:line 434中的CreatePayment(CreatePaymentModel模型)SessionImpl.Flush() 在 NHibernate.Transaction.AdoTransaction.Commit() 在 C3.DataModel.Repositories.NHUnitOfWork.Save() 在 C:\projects\C3\C3.DataModel.Generated\Generated\NHibernateRepositories.generated.cs:line 2659 在 C3.WebUI.Areas.Finance.Controllers.AccountsPayableController.CreatePayment(CreatePaymentModel 模型)在 C:\projects\C3\C3.WebUI\Areas\Finance\Controllers\AccountsPayableController.cs:line 434SessionImpl.Flush() 在 NHibernate.Transaction.AdoTransaction.Commit() 在 C3.DataModel.Repositories.NHUnitOfWork.Save() 在 C:\projects\C3\C3.DataModel.Generated\Generated\NHibernateRepositories.generated.cs:line 2659 在 C3.WebUI.Areas.Finance.Controllers.AccountsPayableController.CreatePayment(CreatePaymentModel 模型)在 C:\projects\C3\C3.WebUI\Areas\Finance\Controllers\AccountsPayableController.cs:line 434
UPDATEThrowing NHibernate into DEBUG mode, I get a bunch of stuff like this:
更新将NHibernate 投入调试模式,我得到了一堆这样的东西:
processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: C3.DataModel.APVendor cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: C3.DataModel.APVendor.Transactions done cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: C3.DataModel.APVendor.Transactions done processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: C3.DataModel.APVendor NHibernate.Event.Default.AbstractFlushingEventListener ERROR Could not synchronize database state with session NHibernate.PropertyValueException: Error dehydrating property value for C3.DataModel.CFAPTransaction.Vendor ---> NHibernate.HibernateException: Unable to resolve property: APVendorId at NHibernate.Tuple.Entity.EntityMetamodel.GetPropertyIndex(String propertyName) at NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetPropertyValue(Object entity, String propertyPath) at NHibernate.Persister.Entity.AbstractEntityPersister.GetPropertyValue(Object obj, String propertyName, EntityMode entityMode) at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session) at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) --- End of inner exception stack trace --- at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityInsertAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) at NHibernate.Engine.ActionQueue.ExecuteActions() at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) C3.WebUI.Areas.Finance.Controllers.AccountsPayableController ERROR C3.WebUI.Areas.Finance.Controllers.AccountsPayableController: No additional information. NHibernate.PropertyValueException: Error dehydrating property value for C3.DataModel.CFAPTransaction.Vendor ---> NHibernate.HibernateException: Unable to resolve property: APVendorId at NHibernate.Tuple.Entity.EntityMetamodel.GetPropertyIndex(String propertyName) at NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetPropertyValue(Object entity, String propertyPath) at NHibernate.Persister.Entity.AbstractEntityPersister.GetPropertyValue(Object obj, String propertyName, EntityMode entityMode) at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session) at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) --- End of inner exception stack trace --- at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityInsertAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) at NHibernate.Engine.ActionQueue.ExecuteActions() at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) at NHibernate.Impl.SessionImpl.Flush() at NHibernate.Transaction.AdoTransaction.Commit() at C3.DataModel.Repositories.NHUnitOfWork.Save() in C:\projects\C3\C3.DataModel.Generated\Generated\NHibernateRepositories.generated.cs:line 2659 at C3.WebUI.Areas.Finance.Controllers.AccountsPayableController.CreatePayment(CreatePaymentModel model) in C:\projects\C3\C3.WebUI\Areas\Finance\Controllers\AccountsPayableController.cs:line 434
Areas.Finance.Controllers.AccountsPayableController 错误 C3.WebUI.Areas.Finance.Controllers.AccountsPayableController:无附加信息。NHibernate.PropertyValueException:脱水 C3.DataModel.CFAPTransaction.Vendor 的属性值时出错 ---> NHibernate.HibernateException:无法解析属性:NHibernate.Tuple.Entity.EntityMetamodel.GetPropertyIndex(String propertyName) at NHibernate.Tuple.Entity 处的 APVendorId .AbstractEntityTuplizer.GetPropertyValue(Object entity, String propertyPath) at NHibernate.Persister.Entity.AbstractEntityPersister.GetPropertyValue(Object obj, String propertyName, EntityMode entityMode) at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor sessionType) at NHibernate。 .ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index,
It does not appear this is occurring when querying the database. I have a feeling it has problems with me creating a bunch of objects, relating them, and then trying to persist them, but that's a pure guess.
查询数据库时似乎没有发生这种情况。我有一种感觉,我创建一堆对象,将它们关联起来,然后尝试保留它们,这有问题,但这纯粹是猜测。
采纳答案by Andres Rodriguez
It's likely that nhibernate is not showing the correct property of error, check the adjacent properties in the mapping file, looking for errors in relationship between data types from your database and data types from .net or repeated columns in properties... also check this link Fluent NHibernate - IndexOutOfRange
nhibernate 可能没有显示正确的错误属性,请检查映射文件中的相邻属性,查找数据库中的数据类型与 .net 中的数据类型或属性中的重复列之间的关系中的错误......也请检查此链接Fluent NHibernate - IndexOutOfRange
回答by Arkadas Kilic
You should check CFAPTransaction mapping, It looks like you wanted to specify one Vendor for each transaction. In this case your mapping has to be like below code.
您应该检查 CFAPTransaction 映射,看起来您想为每个事务指定一个供应商。在这种情况下,您的映射必须类似于下面的代码。
public CFAPTransactionMap()
{
HasOne(x => x.Vendor).ForeignKey("VendorId").Cascade.All();
...
}
回答by COBRA.cH
In my case it was a missing Identity Specification on the SQL-Server.
就我而言,它是 SQL-Server 上缺少的标识规范。
Simple object:
简单对象:
public class Employee
{
public virtual int ID { get; set; }
}
Mapping:
映射:
public class EmployeeMap : ClassMapping<Employee>
{
public EmployeeMap()
{
Id(x => x.ID, map => { map.Generator(Generators.Identity); map.UnsavedValue(0); });
}
}
SQL:
查询语句:
Here is the ID column with the primary key constraint.
这是带有主键约束的 ID 列。
And here you can see the missing Identity Specification, which is causing the problem.

To solve the problem, you have to specify the ID column as IDENTITYi.e.
要解决这个问题,你必须将ID列指定为IDENTITYie
CREATE TABLE EMPLOYEE
(
ID int NOT NULL IDENTITY(0, 1)
);
回答by Willy David Jr
I encountered the same error. This is my sample mappings:
我遇到了同样的错误。这是我的示例映射:
ManyToOne(x => x.objPerson, map => { map.Column("PersonID"); map.NotNullable(false); });
Property(x => x.intPersonID, map => map.Column("PersonID"));
If I tried to persist/save this on my database by populating only the property intPersonIDand making the objPersonnull, this will trigger the dehydrating error on all your properties!
如果我试图通过仅填充属性intPersonID并将其设为objPersonnull来将其持久/保存在我的数据库中,这将触发所有属性的脱水错误!
The reason I am just populating intPersonIDis to prevent querying on the database to get the objPersonbefore saving to the database. Unfortunately, it will trigger an error, so I modified my mappings and corrected with this:
我只是填充的原因intPersonID是为了防止查询数据库以objPerson在保存到数据库之前获取。不幸的是,它会触发一个错误,所以我修改了我的映射并用这个更正了:
ManyToOne(x => x.objPerson, map => { map.Column("PersonID"); map.NotNullable(false); });
Or if I want to prevent querying on the database by getting the whole object, I will just use this mapping instead:
或者,如果我想通过获取整个对象来阻止对数据库的查询,我将只使用此映射:
Property(x => x.intPersonID, map => map.Column("PersonID"));
But combining them is not possible.
但将它们结合起来是不可能的。
回答by Al Lelopath
In my case, the exception was accurately identifying the property that was the cause of the error. I had a many-to-one property that was lacking a cascadedefinition. "save-update"prevents the error:
就我而言,异常是准确识别导致错误的属性。我有一个缺乏cascade定义的多对一属性。"save-update"防止错误:
<many-to-one name="FocusType" cascade="save-update"
class="MyInfrastructure.FocusType, MyInfrastructure">
<column name="FocusTypeId" sql-type="int" not-null="false" />
</many-to-one>
回答by alin
a smart person once told me to add this to my mapping
一个聪明的人曾经告诉我将其添加到我的映射中
.Length(int.MaxValue);

