C# NHibernate-脱水属性值时出错

C# NHibernate-脱水属性值时出错,c#,nhibernate,C#,Nhibernate,在更新期间提交时,我在脱水属性值时出错。我已经搜索过了,与之相似的唯一区别是,在提到的问题中,NHibernate抱怨无法解决财产问题 数据库中已存在IssuingOffice的值,因此不能是引用未保存记录的问题 下面是详细的错误 Test 'Tests.Services.StickerInvoiceServiceTests.update_sticker_info_succeeds' failed: NHibernate.PropertyValueException : Error dehydr

在更新期间提交时,我在脱水属性值时出错。我已经搜索过了,与之相似的唯一区别是,在提到的问题中,NHibernate抱怨无法解决财产问题

数据库中已存在IssuingOffice的值,因此不能是引用未保存记录的问题

下面是详细的错误

Test 'Tests.Services.StickerInvoiceServiceTests.update_sticker_info_succeeds' failed:
NHibernate.PropertyValueException : Error dehydrating property value for
Model.StickerInvoice.StickerIssuingOffice
----> NHibernate.TransientObjectException : object references an unsaved transient 
instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave. Type: Model.IssuingOffice, Entity: Model.IssuingOffice
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.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)
at NHibernate.Action.EntityUpdateAction.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()
UnitOfWork.cs(39,0): at NhRepository.UnitOfWork.Commit()
StickerInvoiceService.cs(73,0): at Services.StickerInvoiceService.UpdateStickerInfo(StickerInvoice entity, IEnumerable`1& brokenRules)
Services\StickerInvoiceServiceTests.cs(131,0): at Tests.Services.StickerInvoiceServiceTests.update_sticker_info_succeeds()
--TransientObjectException
at NHibernate.Engine.ForeignKeys.GetEntityIdentifierIfNotUnsaved(String entityName, Object entity, ISessionImplementor session)
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)
发行办公室的映射

<class name="IssuingOffice" table="IssuingOffice">
<id name="Id">
  <generator class ="hilo">
    <param name ="table">IdGenerator</param>
    <param name ="column">NextHigh</param>
    <param name ="max_lo">2</param>
    <param name ="where">TableKey = 'IssuingOffice'</param>
  </generator>
</id>
<version name="Version" column="Version" />
<property name ="Name" length="150" not-null="true" unique="true" />
<bag name ="IssuedStickers" table ="StickerInvoice" generic="true" inverse="true">
  <key column ="StickerIssuingOfficeId" />
  <one-to-many class ="StickerInvoice"/>
</bag>
CREATE TABLE IssuingOffice(
 Id   int  NOT NULL,
 Name   nvarchar (150) NOT NULL,
 Version   int  NOT NULL,
   CONSTRAINT  PK_IssuingOffice  PRIMARY KEY ( Id  ASC )
 ) 
发票的映射

<class name="StickerInvoice" table="StickerInvoice">
<id name="Id">
  <generator class ="hilo">
    <param name ="table">IdGenerator</param>
    <param name ="column">NextHigh</param>
    <param name ="max_lo">5</param>
    <param name ="where">TableKey = 'StickerInvoice'</param>
  </generator>
</id>
<version name ="Version" />
<property name ="RefNo" length="50" not-null="true" />
<property name ="Period" not-null="true" />
<property name ="Amount" not-null="true"/>
<property name ="DueDate" not-null="true"/>
<property name ="Penalty" not-null="true"/>
<property name ="InvoiceNo" length="50"/>
<property name ="DateIssued" />
<property name ="ReceiptNo" length="50" />
<property name ="DatePaid" />
<property name ="StickerNo" length="50" />
<many-to-one name ="Vehicle" class="Vehicle" column ="VehicleId" />
<many-to-one name ="StickerIssuedBy" class="User" column ="StickerIssuedById" />
<many-to-one name ="StickerIssuingOffice" class="IssuingOffice" column ="StickerIssuingOfficeId" />
CREATE TABLE StickerInvoice(
    Id int NOT NULL,
RefNo nvarchar(50) NOT NULL,
VehicleId int NOT NULL,
DateIssued datetime NOT NULL,
Period datetime NOT NULL,
Amount decimal(18, 0) NOT NULL,
DueDate datetime NOT NULL,
Penalty decimal(18, 0) NOT NULL,
InvoiceNo nvarchar(50) NULL,
ReceiptNo nvarchar(50) NULL,
DatePaid datetime NULL,
StickerNo nvarchar(50) NULL,
StickerIssuedById int NULL,
StickerIssuingOfficeId int NULL,
Version int NOT NULL,
  CONSTRAINT PK_StickerInvoice PRIMARY KEY ( Id ASC )
)  
我尝试了数据类型不匹配或重复属性,但没有找到


任何帮助都将不胜感激。

如果您更仔细地查看您的异常,您将看到以下错误:

NHibernate.TransientObject异常:对象引用未保存的 瞬态实例-在刷新或删除之前保存瞬态实例 将属性的级联操作设置为使其 自动保存。类型:Model.IssuingOffice,实体:Model.IssuingOffice

<class name="IssuingOffice" table="IssuingOffice">
<id name="Id">
  <generator class ="hilo">
    <param name ="table">IdGenerator</param>
    <param name ="column">NextHigh</param>
    <param name ="max_lo">2</param>
    <param name ="where">TableKey = 'IssuingOffice'</param>
  </generator>
</id>
<version name="Version" column="Version" />
<property name ="Name" length="150" not-null="true" unique="true" />
<bag name ="IssuedStickers" table ="StickerInvoice" generic="true" inverse="true">
  <key column ="StickerIssuingOfficeId" />
  <one-to-many class ="StickerInvoice"/>
</bag>
CREATE TABLE IssuingOffice(
 Id   int  NOT NULL,
 Name   nvarchar (150) NOT NULL,
 Version   int  NOT NULL,
   CONSTRAINT  PK_IssuingOffice  PRIMARY KEY ( Id  ASC )
 ) 
基本上,看起来您正试图保存包含一个或多个未保存的
发票
标签的
IssuingOffice
。您需要将设置为
issuengoffice
bag映射

<bag name="IssuedStickers" table="StickerInvoice" 
     generic="true" inverse="true" cascade="save-update">
  <key column="StickerIssuingOfficeId" />
  <one-to-many class="StickerInvoice"/>
</bag>

我通过设置多对一关系部分的只读标志解决了此异常:

References(x => x.Parent).ReadOnly();

当您试图保存Id为0的对象时,会出现此错误。IssuingOffice已存在于数据库中,因此不需要保存或更新。我只想在不影响发卡机构的情况下更新发票上的现有数据。在这些场景中,我真的需要级联吗?那么您可能正在使用StickingInvoice的临时实例,可能在另一个会话中加载。在调查您的上一条评论时,我意识到IssuingOffice的版本最初在数据初始化期间设置为0。将该值更改为1解决了该问题。谢谢你的帮助,我想知道为什么样品没有提到这个简单的修复。我已经浪费了大约2-3天的时间来寻找这个简单的解决方案。