Efcore: Check constraint validation attribute convention

Created on 19 Apr 2019  路  5Comments  路  Source: dotnet/efcore

Suggested by @bricelam:

Attribute | Expression
--- | ---
[Compare("Y")] | X == Y
[MinLength(8)] | X.Length >= 8
[Range(0, 10)] | X >= 0 && X <= 10
[RegularExpression("[0-9A-F]+") | Regexp.IsMatch(X, "[0-9A-F]+")
[DataType(...)] | (predefined regular expressions)

closed-external type-enhancement

Most helpful comment

Would we simply do this in the external check constraint plugin instead? See https://github.com/efcore/EFCore.CheckConstraints/issues/5

All 5 comments

If this enhancement was implemented, would the following case be supported?

When I add the [Range(0, 10)] attribute to my model, the following migration SQL statements will be generated:

ALTER TABLE MyModel
ADD CONSTRAINT CK_Table_Column_Range CHECK (
   Column >= 0 AND Column <= 10
)

@nphmuller Yes, that's how it would work. Though you might need to enable the convention first.

Notes from triage:

  • We should make sure if we do this that it doesn't cause unwanted breaking change or perf impact.
  • Could apply to reverse-engineering or creating migrations.

Would we simply do this in the external check constraint plugin instead? See https://github.com/efcore/EFCore.CheckConstraints/issues/5

Closing, to be done as part of an external plugin (https://github.com/efcore/EFCore.CheckConstraints/issues/5)

Was this page helpful?
0 / 5 - 0 ratings