Reputation: 155
It seems use the Fluent API has more flexibility.
So I choose the way to follow always use the Fluent API to determine the feature it have in db instead use the annotations to.
But here is the problem,I couldn't find the way to set the Minimum length. Is there a method to perform this?
And i notice,there are no much topic discuss this way.Is the Fluent API way popular??
Hope we choose the right side.
Upvotes: 9
Views: 8705
Reputation: 189
Maybe late reply for this question still there is no way to specify using Fluent API.
But We can do from Check constraint using Fluent API to make sure Data length is greater than some length.
builder.HasCheckConstraint("CHK_DocumentLogo_ExtensionHasAtleastOneChar", "(DATALENGTH([Extension]) > 0)");
you need to install the Microsoft.EntityFrameworkCore.Relational NuGet package to use this extension method.
More info on HasCheckConstraint - https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationalentitytypebuilderextensions.hascheckconstraint?view=efcore-5.0
Upvotes: 0
Reputation: 2429
For string property use StringLength
[StringLength(30, MinimumLength = 5)]
public string MyStringProperty { get; set; }
Upvotes: 2
Reputation: 14498
This is impossible at the moment with EF. If you really have to set a minimum length, you can do it with an attribute though:
[MaxLength(10),MinLength(3)]
public string MyProperty {get; set;}
As the first comment under your question already says, it's probably not very common to have minimum length check in databases (never seen it myself), so this will just throw a validation error when you try to enter a value with a length smaller than 3.
Upvotes: 7