Entity Framework Code-First(9.4):DataAnnotations - Required Attribute

Required attribute can be applied to a property of a domain class. EF Code-First will create a NOT NULL column in a database table for a property on which we apply Required 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; }

    [Required]
    public string StudentName { get; set; }

}

As you can see in the above code, we have applied Required attribute to StudentName. So, Code First will create a NOT NULL StudentName column in the Student table as shown below.

时间: 2024-10-04 11:56:39

Entity Framework Code-First(9.4):DataAnnotations - Required Attribute的相关文章

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

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

Entity Framework Code-First(9.2):DataAnnotations - TimeStamp Attribute

DataAnnotations - TimeStamp Attribute: TimeStamp attribute can be applied to only one byte array property of a domain class. TimeStamp attribute creates a column with timestamp datatype. Code-First automatically use this TimeStamp column in concurren

Entity Framework Code-First(9.7):DataAnnotations - Table Attribute

DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First convention creates a table name same as the class name. Table attribute overrides this default convention. EF Code-First will create a table with a speci

Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute

DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. Default Code-First convention creates a column for all the properties which includes getters and setters. NotMapped attribute overrides this default c

Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute

DataAnnotations - ForeignKey Attribute: ForeignKey attribute can be applied to properties of a class. Default Code-First convention for ForeignKey relationship expects foreign key property name match with primary key property. Consider the following

Entity Framework Code-First(9.1):DataAnnotations - Key Attribute

DataAnnotations - Key Attribute: Key attribute can be applied to properties of a class. Default Code-First convention creates a primary key column for a property whose name is "Id" or {Class Name} + "Id". Key attribute overrides this d

Entity Framework Code-First(9.11):DataAnnotations - InverseProperty Attribute

DataAnnotations - InverseProperty Attribute: We have seen in the Code-First Convention section that Code-First creates {Class Name}_{Primary Key} foreign key column if you have not included foreign key property in a parent class. The InverseProperty

Entity Framework Code First (三)Data Annotations

Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表示 EF 所依赖的模型去执行查询.更改追踪.以及更新功能,这意味着你的 domain classes 必须遵循 EF 所使用的约定.然而,如果你的 domain classes 不能遵循 EF 所使用的约定,此时你就需要有能力去增加一些配置使得你的 classes 能够满足 EF 所需要的信息. C

Entity Framework Code First (二)Custom Conventions

------------------------------------------------------------------------------------------------------------ 注意:以下所讨论的功能或 API 等只针对 Entity Framework 6 ,如果你使用早期版本,可能部分或全部功能不起作用! --------------------------------------------------------------------------