9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-in-code-first.aspx

EF 6 Code-First系列文章目录:

MaxLength特性指定了属性的值所允许的最大值,然后在数据库中就生成相应列的最大值。MaxLength特性可以应用于实体的String类型的属性和byte[]数组类型的属性上。

如果MaxLength特性,应用在其他类型的属性上就报错,例如下面的图中例子:
enter description here
enter description here

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

上面代码例子中,MaxLength(50)应用在StudentName属性上,指定StudentName的属性值不能超过50个字符长度。
enter description here

EF将会检查标识了MaxLength特性的属性值,如果长度超过了指定的长度,就会报错,EF6报错:System.Data.Entity.Validation.DbEntityValidationException ,EF Core报错:Microsoft.EntityFrameworkCore.DbUpdateException.

请注意:MaxLength特性,同样可以用在ASP.NET MVC中,用于验证属性的值,了解更多详情请看这篇文章: Implement Validations in ASP.NET MVC

转载于:https://www.cnblogs.com/caofangsheng/p/10677865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值