简体   繁体   English

如何在 Entity Framework 6 Database First 中对导航属性实现乐观并发

[英]How to implement optimistic concurrency on navigation properties in Entity Framework 6 Database First

What's the idiomatic way to implement optimistic concurrency in Entity Framework Database First if you have navigation properties on your entities (Ie x to Many relationship between parent and child tables)?如果您的实体上有导航属性(即父表和子表之间的多关系),那么在 Entity Framework Database First 中实现乐观并发的惯用方法是什么? Do you need to check the rowversion on both parent and child entities or is there wa such that only checking the parent entity's rowversion would suffice to know whether any child table was updated as well as any parent?您是否需要检查父实体和子实体的行版本,或者是否存在这样的情况,仅检查父实体的行版本就足以知道是否更新了任何子表以及任何父表?

Consider 2 tables:考虑 2 个表:

Table Parent {
   ParentId INT PRIMARY KEY,
   ValueParent INT
   Version ROWVERSION
}


Table Child {
   ParentId INT FOREIGN KEY
   ChildId INT,
   ValueChild INT
   Version ROWVERSION
  (ParentId,ChildId) Primary Key
}

When converted to entities, the tables will look like the following:转换为实体后,表格将如下所示:

class Parent{
   int ParentId;
   int valueParent;
   byte[] version;
   virtual ICollection<Child> Children
}

class Child{
   int parentId;
   int childId;
   int valueChild;
   byte[] version;
}

If I load a Parent object, and only update the child entity on it,如果我加载一个Parent object,并且只更新它的子实体,

parent.Children.ElementAt(0).valueChild = 1 //

In the db, the rowversion in the child entity will increment, but the parent rowversion will remain unchanged.在 db 中,子实体中的 rowversion 将递增,但父 rowversion 将保持不变。 Is that valid?那有效吗? If I want to implement optimistic concurrency I'd still need to do a rowversion check on both parent and child to ensure that the model hasn't changed.如果我想实现乐观并发,我仍然需要对父级和子级进行行版本检查,以确保 model 没有改变。 I could set concurrency mode to fixed for versions on both entities but that wouldn't prevent having to check at the beginning before I do the update right?我可以将两个实体上的版本的concurrency mode设置为fixed ,但这不会阻止在我进行更新之前必须在开始时进行检查,对吗?

What's the idiomatic way to implement optimistic concurrency if you have 1-many relationships between your entities?如果您的实体之间有一对多关系,那么实现乐观并发的惯用方法是什么? Is there some way to do it where I only need to check the parent rowversion or would I need always to check rowversions of any child elements as well?有什么方法可以做到这一点,我只需要检查父行版本,还是我总是需要检查任何子元素的行版本?

Optimistic concurrency is checked on every row, and you just need to configure the column as a Timestamp/Rowversion and EF will generate the concurrency checks for you.对每一行进行乐观并发检查,您只需将列配置为Timestamp/Rowversion ,EF 将为您生成并发检查。

Is there some way to do it where I only need to check the parent rowversion有什么方法可以做到,我只需要检查父 rowversion

As @Jeremy Lakeman suggests, if you always update the parent when any child changes, then concurreny violations from your EF app can be detected and prevented.正如@Jeremy Lakeman 所建议的那样,如果您总是在任何孩子发生变化时更新父母,那么可以检测和防止来自您的 EF 应用程序的并发违规行为。 But it wouldn't protect against ad-hoc updates to the child rows.但它不能防止对子行的临时更新。

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

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