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.

Entity Framework Code-First(9.8):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.

Entity Framework Code-First(9.8):DataAnnotations - Column Attribute

相关文章:

  • 2022-02-27
  • 2021-09-21
  • 2021-11-02
  • 2021-08-30
  • 2021-12-19
  • 2021-12-03
  • 2022-12-23
  • 2021-07-24
猜你喜欢
  • 2022-01-13
  • 2022-02-17
  • 2022-02-06
  • 2021-12-21
  • 2022-02-08
  • 2022-01-10
  • 2022-02-28
相关资源
相似解决方案