当前位置: 代码迷 >> .NET相关 >> 数据引语特性-Table
  详细解决方案

数据引语特性-Table

热度:206   发布时间:2016-04-24 02:38:55.0
数据注解特性--Table

大家可能注意到,有几个特性,我没有翻译,因为实在是太简单了,看一下就知道,之前也学过,现在只是系统学一下,所以就粗略的看一下就行了。

现在学习数据注解特性的--Table特性。

Table 特性可以被用到类中,Code--First默认的约定是使用类名称为我们创建表名,Table特性可以重写这个约定,只要我们指定名字,EF就会根据Table属性里面的名字,为我们创建数据表名称。

我们看一下下面的代码吧:

using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF2{    [Table("StudentMaster")]   public class Student    {        [Key]        [Column(Order=1)]        public int StudentKey1 { get; set; }        [Key]        [Column(Order=2)]        public int StudentKey2 { get; set; }                [MaxLength(20)]        [ConcurrencyCheck]        [Required]        public string StudentName { get; set; }        [NotMapped()]        public int? Age { get; set; }        public int StdId { get; set; }        [ForeignKey("StdId")]        public virtual Standard Standard { get; set; }          }}

然后我们运行程序,看一下数据库:

我们看到生成的数据表是Studentmaster。

 

你同样可以指定 表的schema 请看:

using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF2{    [Table("StudentMaster",Schema="WaHaHa")]   public class Student    {        [Key]        [Column(Order=1)]        public int StudentKey1 { get; set; }        [Key]        [Column(Order=2)]        public int StudentKey2 { get; set; }                [MaxLength(20)]        [ConcurrencyCheck]        [Required]        public string StudentName { get; set; }        [NotMapped()]        public int? Age { get; set; }        public int StdId { get; set; }        [ForeignKey("StdId")]        public virtual Standard Standard { get; set; }          }}

 

  相关解决方案