zoukankan      html  css  js  c++  java
  • Entity Framework Code-First(9.6):DataAnnotations

    DataAnnotations - StringLength Attribute:

    StringLength attribute can be applied to a string type property of a class. EF Code-First will set the size of a column as specified in StringLength 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; }
         
        [StringLength(50)]
        public string StudentName { get; set; }
            
    }

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

    dataannotations maxlength attribute

    Entity Framework also validates the value of a property for StringLength 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.

  • 相关阅读:
    html5——渐变
    html5——背景
    html5——边框
    html5——私有前缀
    html5——盒子模式
    html5——文本阴影
    html5——颜色
    html5——css选择器
    html5——DOM扩展
    html5——多媒体(一)
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5644169.html
Copyright © 2011-2022 走看看