Enforcing business rules in entity framework core
Database integrity check is your best friend
Based on your description your appointments are based on slots. This made the problem a lot simpler since you can efficiently define a unique constraint for SlotId
on the Appointments
table. And then you would need a foreign key for Appointments.SlotId
references Slot.Id
what if the calendar slot fetched in 1 is removed before step 3?
DB would throw foreign key violation exception
what if another appointment is booked after step 2 but before step 3?
DB would throw duplicated key exception
What you need to do next is to catch those two exceptions and redirect user back to the booking page. Reload data from DB again and check for any invalid entries, notify the user to make modification and try again.
For the deadlock part it really depends on your table structure. The way you access data, the way you index them, and DB's query plan. Theres no definitive answer to that.