简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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