博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7.5 数据注解特性--MaxLength&&MinLength
阅读量:6593 次
发布时间:2019-06-24

本文共 1493 字,大约阅读时间需要 4 分钟。

MaxLength attribute can be applied to a string or array type property of a domain class. EF Code First will set the size of a column as specified in MaxLength attribute. Note that it can also be used with ASP.Net MVC as a validation attribute.

Consider the following example.

using System.ComponentModel.DataAnnotations;    public class Student{    public Student() { } public int StudentID { get; set; } [MaxLength(50)] public string StudentName { get; set; } }

As you can see in the above code, we have applied MaxLength attribute to StudentName. So, Code-First will create a nvarchar(50) column StudentName in the Student table as shown below.

Entity Framework also validates the value of a property for MaxLength attribute if you set the value more than the specified size. For example, if you set more than 50 chars long StudentName then EF will throw EntityValidationError.

MinLength:

MinLength attribute is a validation attribute. It does not have an impact on the database schema. EF will throw EntityValidationError if you set a value of a string or array property less than the specified length in MinLength attribute.

MinLength attribute can also be used with MaxLength attribute as shown below.

using System.ComponentModel.DataAnnotations;    public class Student{    public Student() { } public int StudentID { get; set; } [MaxLength(50),MinLength(2)] public string StudentName { get; set; } }

In the above example, StudentName can not be less than 2 chars and more than 50 chars.

转载地址:http://vqdio.baihongyu.com/

你可能感兴趣的文章
[转载]SpringMVC的Model参数绑定方式
查看>>
Linux socket多进程服务器框架三
查看>>
Debug.print的用法
查看>>
常用名词
查看>>
第一百三十四节,JavaScript,封装库--遮罩锁屏
查看>>
【转】cookie如何共享到各个浏览器
查看>>
自制基于HMM的python中文分词器
查看>>
如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接
查看>>
RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2-新增锁定用户与解除锁定用户的功能...
查看>>
vue1.0 的过滤器
查看>>
如何删除anaconda
查看>>
Mybatis3.3——源码阅读笔记
查看>>
oracle中的trunc函数操作
查看>>
EventCache表太大, 怎么办?
查看>>
Top 10 mistakes in Eclipse Plug-in Development
查看>>
Directx教程(23) 简单的光照模型(2)
查看>>
Java 并发性和多线程
查看>>
IE6下frameset横向滚动条BUG
查看>>
Python线程专题9:线程终止与挂起、实用工具函数
查看>>
用ASP.NET Core 2.1 建立规范的 REST API -- 翻页/排序/过滤等
查看>>