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

    DataAnnotations - Column Attribute:

    Column attribute can be applied to properties of a class. Default Code First convention creates a column name same as the property name. Column attribute overrides this default convention. EF Code-First will create a column with a specified name in Column attribute for a given property.

    Consider the following example.

    using System.ComponentModel.DataAnnotations.Schema;
    
    public class Student
    {
        public Student()
        { 
            
        }
        public int StudentID { get; set; }
         
        [Column("Name")]
        public string StudentName { get; set; }
            
    }

    As you can see in the above example, Column attribute is applied to StudentName property of Student class. So, Code-First will override default conventions and create Name column instead of StudentName column in the Student table, as shown below.

    dataannotations column attribute

    You can also specify an order and type of the column using Column attribute, as shown below.

    using System.ComponentModel.DataAnnotations.Schema;
    
    public class Student
    {
        public Student()
        { 
            
        }
        public int StudentID { get; set; }
         
        [Column("Name", Order=1, TypeName="varchar")]
        public string StudentName { get; set; }
            
    }

    The above code creates Name column of varchar type as a first column in Student, as shown below.

    dataannotations column attribute

  • 相关阅读:
    一份感动到哭的成绩单……
    永远不要、不要、不要、不要放弃
    FusionChart 保存图片 小强斋
    JfreeChart的使用 小强斋
    JFreeChart中文API 小强斋
    FusionChart 小强斋
    面试题>旋转字符串 小强斋
    Dom4j 小强斋
    FusionChart 保存图片 小强斋
    JFreeChart中文API 小强斋
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5644198.html
Copyright © 2011-2022 走看看