设为首页 加入收藏

TOP

9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
2019-09-17 19:08:24 】 浏览:40
Tags:9.10 翻译 系列 数据 注解 特性 StringLength Code-First

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

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

StringLength特性可以应用于实体的string类型的属性上,它指定了属性的所允许的最大字符长度,然后对应在数据库中就生成相应长度的数据列(在SQL Server数据库中是,nvarchar类型)。

using System.ComponentModel.DataAnnotations;

public class Student
{
    public int StudentID { get; set; }
    [StringLength(50)]
    public string StudentName { get; set; }
}

上面的例子中,我们将StringLength特性应用在StudentName属性上,所以EF将会在StudentName列,映射为nvarchar(50):
enter description here

EF会验证StudentName的属性值的长度,如果大于50个字符长度,就报错:EF 6中:System.Data.Entity.Validation.DbEntityValidationException,EF Core中Microsoft.EntityFrameworkCore.DbUpdateException

请注意:StringLength特性,还可以用在ASP.NET MVC中,用来验证属性的值,了解更多,请看这篇文章:Implement Validations in ASP.NET MVC

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇TaskCreationOptions.LongRunning.. 下一篇[开源]Entity Framework 6 Reposi..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目