问题
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.
回答1:
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.
回答2:
For string property use StringLength
[StringLength(30, MinimumLength = 5)]
public string MyStringProperty { get; set; }
https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute%28v=vs.110%29.aspx
来源:https://stackoverflow.com/questions/34533233/does-the-ef-fluent-api-can-setting-the-minimum-length