简体   繁体   English

ConcurrencyCheck 在第一次更新时抛出 OptimisticConcurrencyException

[英]ConcurrencyCheck throws OptimisticConcurrencyException on first single update

I'm doing a simple update and it works if I won't modify the field that I decorated with [ConcurrencyCheck] attribute.我正在做一个简单的更新,如果我不修改我用[ConcurrencyCheck]属性修饰的字段,它会起作用。

This is my property:这是我的财产:

[Required]
[Index]
[Display(Name="First Name")]
[ConcurrencyCheck]
public string firstname { get; set; }

This is my update action method:这是我的更新操作方法:

[HttpPost]
[Err()]
public async Task<ActionResult> Edit(Entries entry)
 {
     if (ModelState.IsValid)
     {
            CtxRepoRBMS<CarlCtx, Entries> ctxRDMS = HttpContext.GetOwinContext().Get<CtxRepoRBMS<CarlCtx, Entries>>();
            MongoRepo<Items> ctxMongo = new MongoRepo<Items>(client: new MongoClient("mongodb://localhost"), dbName: "items");

            ctxRDMS.Update(entry);
            ctxRDMS.Save();

ctxRDMS.Update is: ctxRDMS.Update是:

public void Update(Cls entry)
{
       ctx.Entry(entry).State = EntityState.Modified;
}

and ctxRDBMS.Save isctxRDBMS.Save

public void Save()
{
   ctx.SaveChanges();
}

The update works fine if i will modify other properties except firstname , but as soon as I change it and save it will throw the OptimisticConcurrencyException which doesn't make sense because there is no one else editing firstname other than me.如果我将修改除firstname之外的其他属性,则更新工作正常,但是一旦我更改它并保存它就会抛出OptimisticConcurrencyException这没有意义,因为除了我之外没有其他人编辑firstname

I think I have just figured this one out.我想我刚刚弄清楚了这一点。

If a column is decorated with [concurrencycheck], that column cannot be changed without throwing a concurrency exception (at least I can't change it).如果一列用 [concurrencycheck] 修饰,那么在不抛出并发异常的情况下无法更改该列(至少我无法更改它)。

None of the documentation states it so clearly and the examples I see usually choose some column in their example that could well be changed over the life of the data (Last Name, in one case).没有一个文档如此清楚地说明它,我看到的示例通常会在其示例中选择一些列,这些列很可能在数据的生命周期内发生变化(在一种情况下为姓氏)。

The consensus seems to be to use a ROWVERSION column in SQL Server as the only concurrency token in the model.共识似乎是使用 SQL Server 中的 ROWVERSION 列作为模型中唯一的并发令牌。

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

相关问题 为简单的获取/更新方法引发的OptimisticConcurrencyException - OptimisticConcurrencyException thrown for a simple fetch/update method 首次更新相关实体后,EF Core 2会在SaveChanges上引发异常 - EF Core 2 throws exception on SaveChanges, after first update for related entities MVC应用程序中的OptimisticConcurrencyException - OptimisticConcurrencyException in MVC application 实体框架代码首先更新复杂类型的单个属性 - Entity Framework Code First Update a Single Property of a Complex Type 如何在MVC应用程序中修复OptimisticConcurrencyException - How to fix OptimisticConcurrencyException in an MVC application ASP.NET MVC OptimisticConcurrencyException - ASP.NET MVC OptimisticConcurrencyException 不抛出OptimisticConcurrencyException:可能的原因:UpdateCommand - OptimisticConcurrencyException is not thrown: Possible reason: UpdateCommand EF Count()&gt; 0,但First()引发异常 - EF Count() > 0 but First() throws exception 当模型属性具有 [ConcurrencyCheck] 属性时,ASP.NET Core MVC ConcurrencyCheck 失败 - ASP.NET Core MVC ConcurrencyCheck is failing when the model property has the [ConcurrencyCheck] attribute enum_in.Single()为1并引发异常 - enum_in.Single() is 1 and throws exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM