简体   繁体   English

ASP.NET Core Web API 和 Entity Framework 同时检查实体是否存在于数据库中

[英]ASP.NET Core Web API and Entity Framework simultaneously check if entity exists in database

I'm developing an ASP.NET Core Web API project using Entity Framework.我正在使用 Entity Framework 开发一个 ASP.NET Core Web API 项目。 API requests are constantly coming from a source and I receive them, I check with linq whether the account number field has been added to the database before, if not, I add it, but sometimes 20 requests can come from the API source at the same time and duplicate records may occur. API请求不断来自一个来源,我收到它们,我用linq检查之前是否已将帐号字段添加到数据库中,如果没有,我添加它,但有时20个请求可以同时来自API源时间和重复记录可能会发生。

How can I prevent this situation?我怎样才能防止这种情况?

var account = dbContext.Accounts
                       .FirstOrDefault(x => x.AccountNumber == apiMessage.AccountNumber);

if (account is null)
{
    account = new Account();
    account.AccountNumber = apiMessage.AccountNumber;
}

Just handle it in database side to make it easy.只需在数据库端处理它就可以了。 Add Unique constraint in the AccountNumber column.在 AccountNumber 列中添加唯一约束。

You can handle the migration in your EF configuration您可以在 EF 配置中处理迁移

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<Account>()
        .HasIndex(u => u.AccountNumber)
        .IsUnique();
}

You can wait for the controller to finish before starting a new one (No concurrent calls), but that will not be a good solution.您可以等待控制器完成后再开始新的控制器(无并发调用),但这不是一个好的解决方案。

If you do that, the requests will either wait for their turn, which decreases performance.如果您这样做,请求将等待轮到它们,这会降低性能。

Or they will not run at all if the controller is already working, which will result in a controller not answering all the requests.或者,如果控制器已经在工作,它们根本不会运行,这将导致控制器无法响应所有请求。

暂无
暂无

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

相关问题 ASP.NET 核心 Web API 使用实体框架核心 - ASP.NET Core Web API using Entity Framework Core ASP.NET Core 3.1 实体框架存在查询持续时间 - ASP.NET Core 3.1 Entity Framework EXISTS Query Duration Entity Framework Core 和 ASP.NET Core MVC 中的 Inheritance 数据库 - Inheritance Database in Entity Framework Core and ASP.NET Core MVC ASP.NET Core MVC 1.3 Web API + Entity Framework,我无法按名称搜索数据库,但可以按 ID - ASP.NET Core MVC 1.3 Web API + Entity Framework, I can't search a database by name, but I can by id 如何检查我的数据库中是否已经存在一个值并在ASP.NET和实体框架中显示验证消息 - How to check if a value already exists in my database and show a validation message in ASP.NET & Entity Framework ASP.NET Core Web API &amp; Entity Framework Core; 为什么我收到这个错误? - ASP.NET Core Web API & Entity Framework Core; why am I getting this error? ASP.NET 核心 Web API 与实体框架核心默认脚手架 Z594C103F2C6E04C3DAZ8AB05E 混淆 put? - ASP.NET Core Web API with Entity Framework Core default scaffolded controller confuses put and post? 用谷歌授权 asp.net 核心 web api(实体框架核心) Z7C82E852FB415F250Z92 - authorize asp.net core web api (entity framework core) with google oauth Update-Database 命令在 ASP.Net Core / Entity Framework Core 中不起作用,因为数据库中的对象已经存在 - Update-Database command is not working in ASP.Net Core / Entity Framework Core because object in database already exists 使用实体框架在ASP.Net Core 2中动态创建数据库表 - Dynamic database table creation in ASP.Net Core 2 with Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM