Does the EF Fluent Api can setting the Minimum Length?

时光毁灭记忆、已成空白 提交于 2020-06-27 08:23:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!