Adding a JSON CHECK CONSTRAINT
for a table field with
ALTER TABLE [dbo].[Data]
ADD CONSTRAINT [JsonData must be formatted as JSON]
CHECK (IsJson([JsonData]) > 0)
works fine, but I want to make it work for Code First.
I have tried the Reverse Engineering Code First
, but it does not help me with this problem. Executing a Sql Command with the same code (Seed() method)
works very well, but this is not one of the solutions I would like to use:
protected override void Seed(MyContext context)
{
context
.Database
.ExecuteSqlCommand(
"ALTER TABLE [dbo].[Data]
ADD CONSTRAINT [JsonData must be formatted as JSON]
CHECK (IsJson([JsonData]) > 0)");
}
Is there any other way I can add a JSON Check Constraint
from Code First?