7.7 数据注解特性--Table

2018-06-22 01:05:05来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

现在学习数据注解特性的--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; }

      

    }
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Visual Basic 2012 借助DataGridView控件将SQL server2012 数据

下一篇:Nancy之基于Nancy.Owin的小Demo