Data Annotation Feature-Table, Annotation Feature-table
You may have noticed that there are several features that I have not translated, because it is too simple. You can see it at a Glance. I have also learned it before. Now it's just a systematic study, so just take a rough look.
Now we are learning the Table feature of the Data Annotation Feature.
The Table feature can be used in classes. The default convention of Code-First is to use the class name to create a Table name. The Table feature can override this convention, as long as we specify the name, EF creates a data Table name based on the name in the Table attribute.
Let's take a look at the following code:
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; } }}
Then run the program and check the database:
The generated data table is Studentmaster.
You can also specify the table schema. See:
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; } }}