I’m new in Entity Framework and i have problem with something which was quite easy in pure t-sql. Assume that we have table with 3 columns. Id(identity, primary key), IdKind (int), Number(int). Column Nubmer depends on IdKind column. Data in this table should look like this:
Id | IdKind | Number
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 1
5 | 2 | 2
6 | 2 | 3
As you can see column number is auto incremented depends on IdKind. In t-sql I was finding max(Number) + 1 for some IdKind. Everything was in one stored procedure in one transaction so value of Number was unique for IdKind.
How to accomplish the same in Entity Framework. Value of Number should be computed when creating new object or when changing IdKind.
I would use database trigger for that and configure
Numberproperty in EF mapping withStoreGeneratedPattern.Computedto inform EF that this property is filled in the database and must be reloaded after each update / insert.I would also not use max() for each new number. Instead I would maintain separate table simulating sequences. This table should contain record per sequence – in your case record per
IdKindwith current max number. Instead of selecting aggregation you will select single record and update it to contain new max number. Working with this table must be in the same transaction as persisting changes to your entity.