EF Fluent Api是否可以设置最小长度? [英] Does the EF Fluent Api can setting the Minimum Length?

查看:110
本文介绍了EF Fluent Api是否可以设置最小长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎使用Fluent API具有更大的灵活性。



因此,我选择遵循的方式始终使用Fluent API来确定它在db中具有的功能,而不是使用



但这是问题,我找不到设置最小长度的方法。
有执行此操作的方法吗?



我注意到,这种方式讨论的话题不多。FluentAPI受欢迎吗? p>

希望我们选择右侧。

解决方案

EF的时刻。如果确实需要设置最小长度,则可以使用属性来实现:

  [MaxLength(10),MinLength (3)] 
公用字符串MyProperty {get; set;}

正如您所提问题的第一条评论所述,最低数据库中的长度检查(我自己从未见过),因此当您尝试输入长度小于3的值时,这只会引发验证错误。


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.

解决方案

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.

这篇关于EF Fluent Api是否可以设置最小长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆