简体   繁体   English

EF强制并发检查

[英]EF Forced Concurrency Checks

I have an issue with EF 4.0 that I hope someone can help with. EF 4.0出现问题,希望有人可以提供帮助。 I currently have an entity that I want to update in a last in wins fashion (ie ignore concurrency checks and just overwrite whats in the db with what is submitted). 我目前有一个实体,我想以wins的方式进行最后更新(即忽略并发检查,只是用提交的内容覆盖数据库中的内容)。 It seems Entity Framework not only includes the primary key of the entity in the where clause of the generated sql, but also any foreign key fields. 看来Entity Framework不仅在所生成的sql的where子句中包括实体的主键,而且还包括任何外键字段。 This is annoying as it means that I don't get true last in wins semantics and need to know what value the fk field had before the update or I get a concurrency exception. 这很烦人,因为这意味着我在wins语义上没有获得真正的成功,需要在更新之前知道fk字段具有什么值,否则我将获得并发异常。

I am aware that this can be short circuited by including a foreign key field as well as the navigation property on the entity. 我知道可以通过在实体上包含外键字段以及导航属性来实现短路。 I would like to avoid this if possible as it's not a very clean solution. 我想避免这种情况,因为这不是一个很干净的解决方案。

I was just wondering if there was any other way to override this behaviour? 我只是想知道是否还有其他方法可以替代此行为? It seems like more of a bug than a feature. 似乎更多的是错误而不是功能。 I have no problem with ef doing concurrency checks if I instruct it to do so but not being able to bypass concurrency completely is a bit of a hindrance as there are many valid scenarios where this is not needed 如果我指示执行并发检查,我对ef没问题,但是不能完全绕过并发是一个障碍,因为在很多情况下不需要这样做

I am not 100% sure this will work, but I was curious myself and think I might have found a solution. 我不是100%肯定会成功,但是我很好奇自己,认为我可能已经找到了解决方案。

What if you set the [ConcurrencyCheck] attribute on a field that is immutable (in my case I used the ID (Primary Key). Since that will never be changed it will never trigger a concurrency exception and therefore clobber your entry. 如果您在不可变的字段上设置[ConcurrencyCheck]属性,该怎么办(在我的情况下,我使用的是ID(主键)。因为它永远不会更改,因此永远不会触发并发异常,因此会破坏您的输入。

Give it a whirl and see if that solves your issue. 旋转一下,看看是否能解决您的问题。

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

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