Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add CHECK constraint with fluent API in EF7?

Is it possible to add CHECK constraint with fluent API in Entity Framework 7?

I need to acheive something like this:

...
ADD CONSTRAINT CK_SomeTable_SomeColumn CHECK (SomeColumn >= X);

It is ok if solution is provider-specific - I am targeting MsSqlServer only (at least now).

like image 717
rtf_leg Avatar asked Dec 12 '15 21:12

rtf_leg


1 Answers

As of EF Core 3.0, you can use

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<SomeTable>(entity =>
        entity.HasCheckConstraint("CK_SomeTable_SomeColumn", "[SomeColumn] >= X");
}
like image 168
Ryan Sparks Avatar answered Feb 01 '23 09:02

Ryan Sparks