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 specified name in Table attribute for a given domain class.
Consider the following example.
using System.ComponentModel.DataAnnotations.Schema; [Table("StudentMaster")] public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } }
As you can see in the above example, Table attribute is applied to Student class. So, Code First will override default conventions and create StudentMaster table instead of Student table as shown below.
You can also specify a schema for the table using Table attribute as shown below.
using System.ComponentModel.DataAnnotations.Schema; [Table("StudentMaster", Schema="Admin")] public class Student { public Student() { } public int StudentID { get; set; } public string StudentName { get; set; } }
Code-First will create StudentMaster table in Admin schema as shown below.
本文介绍了如何使用DataAnnotations中的TableAttribute来自定义EF Code-First创建的数据表名称及架构。通过示例展示了如何覆盖默认的表名和指定表所在的数据库架构。


225

被折叠的 条评论
为什么被折叠?



