I need to add constraint only for positive number (0-...) and restrict numbers as (...-1).
CreateTable(
"dbo.Drinks",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(),
ImageUrl = c.String(),
Price = c.Int(nullable: false),
Count = c.Int(nullable: false)
})
.PrimaryKey(t => t.Id);
//.Constraint ???
Something like 'unsigned' in MS SQL. Thank you.
Entity Framework fluent configuration doesn't currently support min or max value settings, just length.
You can, though, implement validation like this, throught annotations:
[Range(1, int.MaxValue, ...)]
int YourInt{ get; set; }
But this will only incur in validation during save, not the actual constraint being added to the database as it would with the fluent configuration / migration.
PS
I love Entity Framework migrations but I only use it during development until the first release. It's a huge time saver until you actually ship your product. After that, I usually fine tune the database myself, including indexes and constraints, and drop using EF migrations altogether.
Databases should be clinically adjusted in production because they're usually one the most (if not the most) important factors in performance and optimization. And unfurtunately EF migrations don't give you all the flexibility you need, even though it covers the most important use-cases.
Maybe with a Range Dataannotation:
[Range(0.0, Double.MaxValue)]
Only values over 0 is what you are looking for, aren't you?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With