简体   繁体   English

有没有一种方法可以防止EF6恢复保存后的实体上的属性?

[英]Is there a way of preventing EF6 from reverting a property on an entity post Save?

I am using CodeFirst and EF6 with individual mappings for states. 我使用CodeFirstEF6与各州单独的映射。

I have a weird scenario that is as follows: 我有一个奇怪的情况如下:

  1. Create a new till entity ( state is 'Added' ). 创建一个新的直到实体( 状态为'Added' )。
  2. Assign a closing value to the new till, ie '99.01m' . 为新的耕种分配一个结束值,即'99 .01m'
  3. Save it to DB (till saved successfully). 将其保存到数据库(成功保存)。
  4. Create a new order object 创建一个新的订单对象
  5. Create order items object/s 创建订单商品对象
  6. Update till closing values to reflect successful order, ie '101.01m'. 更新直到结算值以反映成功的订单,即“ 101.01m”。
  7. Save the order object to DB ( state is 'added' and saved successfully ). 将订单对象保存到数据库( 状态为“已添加”并成功保存 )。
  8. After save, inspect closing value. 保存后,检查收盘价。
  9. Observe till closing values, expected 101.01 is actually 99.01m 观察直到收盘 ,预期的101.01实际上是99.01m

If I explicitly mark the till: 如果我明确标记直到:

Cache.TodaysCashDraw.ObjectState = ObjectState.Unchanged;

The value is reverted regardless of applying an object state or not. 无论是否应用对象状态,都将还原该值。

Cache.TodaysCashDraw.ObjectState = ObjectState.Modified;

However if I mark it as modified then 99.01m is committed into the DB. 但是,如果我将其标记为已修改,则将99.01m提交到数据库中。

So basically whatever changes appear on my till object are getting reverted, now this is fine but i would like an explanation as to why? 因此,基本上,直到我的耕作对象上出现的任何更改都将被还原,现在可以了,但是我想解释为什么? Ideally I would like to the changes on my till object to be persisted and not reverted as shown above. 理想情况下,我希望持久化对象上的更改保持不变,而不要像上面显示的那样还原。 As the till is going to change numerous times during the working day so I would like to commit changes for a till only when necessary. 由于在工作日内耕作机将多次更改,因此我只想在必要时进行耕作机更改。 Orders and Items should get saved straight away but the Till Object in memory just needs to reflect the orders that have been processed against it. 订单和物料应立即保存,但内存中的Till对象仅需要反映已针对该订单处理的订单。 That's my current thinking and approach which worked well in EF4 and DB First. 这是我目前的想法和方法,在EF4和DB First中效果很好。

Does anybody know what's going on here and what is the ideal way of solving this problem? 有人知道这里发生了什么,解决此问题的理想方法是什么?

A lame backup plan is to commit the till changes straight away. me脚的备份计划是立即执行直到更改为止。

One way would be in a disconnected scenario is to detach the entity in question: 在断开连接的情况下,一种方法是分离有问题的实体:

            // detach the till to prevent it from being tracked.
            Cache.TodaysCashDraw.ObjectState = ObjectState.Detached;

Then when you need to save its state, just re-attach it: 然后,当您需要保存其状态时,只需重新附加它即可:

           _ctx.Context.Set<CashDrawsMony>().Attach(_till);

This solves my problem completely. 这完全解决了我的问题。

On a more humerous note, I was actually looking at the Attach method, wondering what it was for? 更糟糕的是,我实际上正在查看Attach方法,想知道它的用途是什么? = ) =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM