Answer the question
In order to leave comments, you need to log in
How to make a limit on the number of rows per field in a sql server table?
There is a table
Id TypeId Name
1 1 Test
2 1 Test2
There is an Asp.Net application using EF Code First.
From application 2 in this table in different flows are fulfilled.
It is necessary that the table has a restriction on the TypeId field for 3 rows.
That is, if 2 queries are executed in parallel, then the first one to be executed will get into the table, and the second should get an error.
How can this be implemented?
There are options both on the EF side and on the SQL Server side.
Answer the question
In order to leave comments, you need to log in
A relational database does not (and should not) track dependencies between rows in the same table. It is necessary to use synchronization through mutexes / critical sections on the .Net side, or blocking on the SQL Server side.
The second is implemented like this:
1. begin tran
2. select @count = count(*) from table with (tablockx)
3. if @count < 3 insert into ...
4. commit
Another table can be used for step 2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question