简体   繁体   English

实体框架中的事务处理

[英]Transaction handling in Entity Framework

First of all, I do know that Entity Framework has some default transaction handling.首先,我知道实体框架有一些默认的事务处理。

I am currently working on a scheduling tool.我目前正在开发一个调度工具。 Now my question: how can I ensure when two users try to book the same appointment at the same time that only one of them occurs in the database?现在我的问题是:当两个用户尝试同时预订同一个约会时,我如何确保其中只有一个出现在数据库中?

This is my code without checking anything.这是我的代码,没有检查任何内容。

    public async Task<Appointment> CreateAppointmentAsync(Appointment appointment)
    {
        var entityEntry = _context.Appointments.Add(appointment);
        await _context.SaveChangesAsync();
        return entityEntry.Entity;
    }

Thanks for your help.谢谢你的帮助。

how can I ensure when two users try to book the same appointment at the same time that only one of them occurs in the database?当两个用户尝试同时预订同一个约会时,我如何确保其中只有一个出现在数据库中?

The database schema should prevent this from happing.数据库模式应该防止这种情况发生。 Typically you simply need a unique index that prevents double-booking.通常,您只需要一个防止重复预订的唯一索引

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

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