简体   繁体   English

如何在EF Core上处理并发

[英]How to deal with concurrency on EF Core

I have a method that searches for an entity on database and attempts to create it if it doesn't exist. 我有一种方法可以搜索数据库中的实体,如果不存在则尝试创建它。 As an example: 举个例子:

public async Country FindOrCreate(string name)
{
    var country = _context.Countries.FirstOrDefault(p => p.Name == name);

    if (country != null)
        return country;

    country = new Country
    {
        Name = name
    };

    _context.Countries.Add(country);
    _context.SaveChanges();

    return country;
}

The problem is: there are several tasks in background calling FindOrCreate concurrently. 问题是:后台有多个任务同时调用FindOrCreate I have prevented duplicates from being inserted by creating an unique index, but the following happens, causing unwanted exceptions: 我已通过创建唯一索引来防止插入重复项,但是会发生以下情况,从而导致意外的异常:

  1. Task A tries to find Country and fails 任务A尝试找到国家,但失败
  2. Task B tries to find Country and fails 任务B尝试找到国家,但失败
  3. Task A tries to create Country and succeeds 任务A尝试创建国家并成功
  4. Task B tries to create Country and fails 任务B尝试创建国家/地区,但失败

What would be the appropriate way of handling these scenarios of concurrency? 处理这些并发方案的合适方法是什么? Should I work with locks using C# code? 我应该使用C#代码处理锁吗? Or should I set a transaction? 还是应该进行交易? TIA TIA

Handling concurrency at the DB level using unique index is the right approach here. 在这里,使用唯一索引在数据库级别处理并发是正确的方法。 Unique index will guarantee uniqueness of countries no matter what. 唯一索引无论如何都将保证国家的唯一性。

Don't bother locking the C# code, it will not work as soon as you've got more than 1 server running your app (these days it is likely). 不要费心锁定C#代码,一旦您有多台服务器运行您的应用程序(这可能是几天),它就不会起作用。 Transactions in this case are tricky business so I wouldn't bother. 在这种情况下,交易是一件棘手的事情,所以我不会打扰。

How to handle those exceptions: 如何处理这些异常:

In case your task failed when creating country, catch the exception and retry. 如果创建国家/地区时任务失败,请捕获异常并重试。 If you failed to get the country second time for some reason, log the exception and fail. 如果由于某种原因第二次无法获得该国家/地区,请记录该异常并失败。

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

相关问题 如何处理 EF Core 数据库迁移失败? - How to deal with EF Core database migration failures? 如何使用 EF Core 更新具有并发控制的行? - How to update row with concurrency control using EF Core? 如何使用ForNpgsqlUseXminAsConcurrencyToken创建的EF Core并发令牌 - How to use EF Core concurrency token created by ForNpgsqlUseXminAsConcurrencyToken 如何使用EF Core在开发期间处理数据库更改? - How to deal with database changes during development with EF Core? 使用多个实例时的 EF 核心并发 - EF core concurrency when using multiple instances EF CORE没有时间戳的乐观并发 - EF CORE Optimistic concurrency without timestamp XUnit测试的EF Core DbContext并发问题 - EF Core DbContext Concurrency Issue with XUnit Tests 如何在带有 Razor 页面的 ASP.NET Core 2.1 中使用或处理不是为并发而设计的服务器端 SDK - How to Use or Deal With Server-Side SDKs not Designed for Concurrency in ASP.NET Core 2.1 with Razor Pages 使用 EF Core 在 SQL Server 中处理乐观并发 - 更新子级时,如何更新父级? - Handle Optimistic Concurrency in SQL Server with EF Core - when updating child, how to update the parent? 在 EF Core 和 ASP.NET Core 中处理并发的最佳实践? - Best practice to handle concurrency in EF Core and ASP.NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM