Обеспечение максимального количества связей в одновременных запросахC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Обеспечение максимального количества связей в одновременных запросах

Сообщение Anonymous »


I have these entities in my business:

class Team { public int Id { get; set; } public string Name { get; set; } public List Members { get; set; } } class Member { public int TeamId { get; set; } public int UserId { get; set; } } The business rule is that a team cannot have more than 10 members.

So you can add a constraint in your controller or handler like this: AddTeamMemberHandler.cs:

var team = dbContext.Teams .Include(x => x.Members) .FirstOrDefault(x => x.Id == 123); if (team.Members.Count >= 10) throw new Exception("Cannot add more members"); However, when you have multiple users trying to add users at the same time, and the current number of members in the team is 9, all of the request will pass the validation. This can lead to the number of members in the team exceeding 10.

I think it is possible to add a custom SQL constraint in the EF model configuration like this:

modelBuilder.Entity() .HasCheckConstraint("Team_MaxMembers", $"COUNT(\"Members\")

Источник: https://stackoverflow.com/questions/769 ... t-requests
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»