How to generate validation checks for constraints in Entity Framework?

I have my first database script, so EF generates entity classes from an existing database, tables in my db have many check constraints that validate that the inserted / updated values ​​meet certain criteria, my problem is that EF does not work 'generate any code for these constraints, so these constraints will only be checked against the database. I want these checks to be done from the generated classes, so if an error occurs, it will be raised without accessing the database.

I know that I can change the T4 template for my model, so I add my constraints to the generated classes, but any code I write will be hardcoded for each specific table in hand based on the constraint code template, which is what I need. is a solution that adapts the generation to any form or pattern of constraint code. To explain more, here are 2 samples from two different constraints:

([ADT_OPT_UPD]>=(0) AND [ADT_OPT_UPD]<=(2))
([INS_USR]>=(0))

      

These are two examples of constraints I have in my database, and it can get complicated by adding more ORs ORs etc., so I need a generation method that adapts to any validation constraint pattern.

I was thinking about writing a parser that parses the constraint and generates the equivalent C # code, but such a parser would have to handle all possible cases for the shape of the constraint code, and this is too much for me to do now as I don't have time for that ... So, is there another solution, and if this is the only way to do it, is there any tool or library that can help me with this task?

+3


source to share





All Articles