EF Core 模型优先——根据类对象创建数据表

需要的nuget包

  •   Microsoft.EntityframeworkCore.SqlServer  (根据自己的数据库类型选择对应的nuget包)
  •   Microsoft.EntityframeworkCore.Tools
  •   Microsoft.VisualStudio.Web.CodeGeneration.Design

说明:

(1)本文测试使用的数据库是SqlServer2017; 

(2)如果没有Microsoft.EntityFrameworkCore.Design,在PM上执行命令会失败(如下图)
PM> add-migration initicial
Build started...
Build succeeded.
Your startup project 'CoreDemo' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

两个类:Grade(年级)、Student(学生)

一个枚举:Gender(性别)

    /// 
    /// 性别枚举  
    /// 
    public enum Gender
    {
        //一定要写上枚举对应的数字值 
         
        男 = 1,
        女 = 2,
    }


    /// 
    /// 年级实体类
    /// 
    public class Grade
    {
        /// 
        /// 年级Id是主键
        /// 
        public int Id { get; set; }

        /// 
        /// 年级名称
        /// 
        public string Name { get; set; }

        /// 
        /// 年级里的学生
        /// 
        public List Students { get; set; } = new List();
    
    }

    /// 
    /// 学生实体类
    /// 
    public class Student
    {
        /// 
        /// 学生编号
        ///  
        public int Id { get; set; }

        /// 
        /// 学生姓名
        ///  
        public string Name { get; set; }

        /// 
        /// 性别
        /// 
        public Gender Gender { get; set; }

        /// 
        /// 所在年级编号
        ///  
        public int GradeId { get; set; }

        /// 
        /// 所在年级
        /// 
        public Grade Grade { get; set; }
 
        /// 
        /// 默认联系电话
        ///  
        public string? Tele { get; set; }

        /// 
        /// 联系电话2
        ///  
        public string? Tele2 { get; set; }

    }

关系

  1. 一个年级有多个学生,一个学生只能属于一个年级
  2. 每个学生都必须录入性别信息


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        { 
            //数据库连接字符串
            string connStr = "Data Source=127.0.0.1;Initial Catalog=EFDemoTestDB;User ID=sa;Password=123456;TrustServerCertificate=true";
             
            optionsBuilder.UseSqlServer(connStr);

            //设置输出EF Core背后执行的SQL语句到控制台
            optionsBuilder.LogTo(Console.WriteLine);
            base.OnConfiguring(optionsBuilder); 
        }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            //设置Grade对应的数据表
            modelBuilder.Entity(x => {

                x.ToTable("T_Grade"); //设置表名称
                x.HasKey(x => x.Id); //设置主键
                x.Property(c => c.Name).HasColumnType("nvarchar(10)").IsRequired();//设置字段类型 
            });

            //设置Student对应的数据表
            modelBuilder.Entity(x => {

                x.ToTable("T_Student");
                x.HasKey(c => c.Id);
                x.Property(c => c.Name).HasMaxLength(10).IsRequired().IsUnicode();

                //设置Tele Tele2字段是varchar(11) null 类型
                x.Property(c => c.Tele).HasMaxLength(11).IsRequired().IsUnicode(false).IsRequired(false);
                x.Property(c=>c.Tele2).HasMaxLength(11).IsRequired().IsUnicode(false).IsRequired(false);

                //设置Gender列
                x.Property(c => c.Gender)
                .HasConversion(c => (int)c, y => (Gender)Enum.ToObject(typeof(Gender), y))//Gender列存储Enum对应的int值
                .IsRequired();//Gender列不能为空


                //设置GradeId字段是外键 Grade与Student是一对多关系
                x.HasOne(c => c.Grade).WithMany(y => y.Students).HasForeignKey(z => z.GradeId); 
            }); 

            base.OnModelCreating(modelBuilder);
        }

生成数据表

        主要执行的语句:

  • PM> add-migration initicial 
  • PM> update-database 
PM> add-migration initicial
Build started...
Build succeeded.
To undo this action, use Remove-Migration.

PM> update-database
Build started...
Build succeeded.
Done.

查看生成的对应数据库表

EF Core 模型优先——根据类对象创建数据表_第1张图片

你可能感兴趣的:(.netcore,EF,EF,Core)