【译】第14节---数据注解-MaxLength/MinLength
MaxLength
MaxLength属性可以应用于域类的字符串或数组类型属性。 EF Code First将设置MaxLength属性中指定的列的大小。 请注意,它也可以与ASP.Net MVC一起用作验证属性。
请看以下示例:
using System.ComponentModel.DataAnnotations; public class Student
{
public Student()
{ }
public int StudentID { get; set; } [MaxLength()]
public string StudentName { get; set; } }
如上面的代码所示,我们已经将MaxLength特性应用于StudentName属性。 所以,Code-First将在Student表中创建一个nvarchar(50)列Student Name,如下所示:
如果将值设置为大于指定大小,则EF还会验证MaxLength属性的属性值。
例如,如果您设置了超过50个字符长StudentName,则EF将抛出EntityValidationError。
MinLength
MinLength属性是一个验证属性。 它对数据库模式没有影响。 如果在MinLength属性中设置小于指定长度的字符串或数组属性的值,则EF将抛出EntityValidationError。
MinLength属性也可以与MaxLength属性一起使用,如下所示:
using System.ComponentModel.DataAnnotations; public class Student
{
public Student()
{ }
public int StudentID { get; set; } [MaxLength(),MinLength()]
public string StudentName { get; set; } }
在上面的例子中,StudentName不能少于2个字符,不能超过50个字符。
【译】第14节---数据注解-MaxLength/MinLength的更多相关文章
- 【译】第15节---数据注解-StringLength
原文:http://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-code- ...
- 【译】第20节---数据注解-InverseProperty
原文:http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-co ...
- 【译】第19节---数据注解-NotMapped
原文:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-fir ...
- 【译】第18节---数据注解-ForeignKey
原文:http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-fi ...
- 【译】第17节---数据注解-Column
原文:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-first. ...
- 【译】第16节---数据注解-Table
原文:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.a ...
- 【译】第13节---数据注解-Required
原文:http://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-firs ...
- 【译】第12节---数据注解-ConcurrencyCheck
原文:http://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-in-c ...
- 【译】第11节---数据注解-TimeStamp
原文:http://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code-fir ...
随机推荐
- JVM探秘3---垃圾回收机制详解
众所周知,Java有自己的垃圾回收机制,它可以有效的释放系统资源,提高系统的运行效率.那么它是怎么运行的呢,这次就来详细解析下Java的垃圾回收 1.什么是垃圾? 垃圾回收回收的自然是垃圾,那么jav ...
- git 提交命令
git stash -u 占存本地版本 git commit git fetch 提交 git rebase git stash pop 将本地没有提交的代码暂存,然后切换到其他分支,然后再回到当前分 ...
- AtCoder Beginner Contest 085(ABCD)
A - Already 2018 题目链接:https://abc085.contest.atcoder.jp/tasks/abc085_a Time limit : 2sec / Memory li ...
- oj练习---dp专题
1.POJ 3744 Scout YYF I 经典的dp模型,但是要用到快速矩阵幂加速,分段的思想 # include <stdio.h> # include <algorithm& ...
- GoldenGate 12.3发布
新特性: oracle db1. 支持12.2 oracle db2. 支持微服务架构, 可以使用restful api 管理OGG3. Parallel replicat,性能比integrated ...
- The logback manual #03# Configuration
索引 Configuration in logback Automatically configuring logback Automatic configuration with logback-t ...
- python面向对象三大特性之一继承、多态、封装
继承,即在定义一个类时,以另一个类为参数,则称这个新定义的类继承了参数类,父类又称为基类. 单继承表示只继承一个类,多继承表示继承多个类. class parent1: pass class pare ...
- 10: VMware中扩展根分区
1.1 添加一块硬盘 1.先给VMware添加一块60G硬盘 2.必须重启虚拟机才能识别到新加磁盘 fdisk -l # 查看刚刚添加的硬盘 3.查看当前磁盘使用情况 df -hl ...
- kaggle竞赛_mnist_10%
主要是通过mnist了解kaggle的操作细节,最终这里的结果为: 引入必须的库¶ import pandas as pd import numpy as np import matplotlib ...
- maven项目更新之后,JDK版本成为1.5
描述:maven项目更新之后,JDK版本成为1.5? 解决:在pom.xml文件中配置java版本,选中build path 设置之后,刷新maven项目