I have entities in my application that require a unique human friendly identifier that include a sequence integer unique to a subset. For example, in project management software you might have a project called "SUPPORT" and then cases related to this project are automatically sequentially numbered as "SUPPORT-1", "SUPPORT-2", etc.
Q: How can I get a unique identifier that is conditional to other properties on the entity instance?
I have (temporarily) implemented this in my application by overriding the DbContext.SaveChanges()
method, check the latest value in the database for cases related to the project and then create a string with the project name and the sequence number. This doesn't seem robust and I am looking for a proper pattern.
Researching patterns I have found the modelBuilder.HasSequence<int>()
method in the EF Core documentation but I don't see how I can implement this such that is conditional on the value of the project (so a unique sequence for cases in "SUPPORT", not overlapping with cases in "SOFTWARE" project).
I am currently using Entity Framework Core 3.1.