需要的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(性别)
/// <summary>
/// 性别枚举
/// </summary>
public enum Gender
{
//一定要写上枚举对应的数字值
男 = 1,
女 = 2,
}
/// <summary>
/// 年级实体类
/// </summary>
public class Grade
{
/// <summary>
/// 年级Id是主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 年级名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 年级里的学生
/// </summary>
public List<Student> Students { get; set; } = new List<Student>();
}
/// <summary>
/// 学生实体类
/// </summary>
public class Student
{
/// <summary>
/// 学生编号
/// </summary>
public int Id { get; set; }
/// <summary>
/// 学生姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 性别
/// </summary>
public Gender Gender { get; set; }
/// <summary>
/// 所在年级编号
/// </summary>
public int GradeId { get; set; }
/// <summary>
/// 所在年级
/// </summary>
public Grade Grade { get; set; }
/// <summary>
/// 默认联系电话
/// </summary>
public string? Tele { get; set; }
/// <summary>
/// 联系电话2
/// </summary>
public string? Tele2 { get; set; }
}
关系:
- 一个年级有多个学生,一个学生只能属于一个年级
- 每个学生都必须录入性别信息
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<Grade>(x => {
x.ToTable("T_Grade"); //设置表名称
x.HasKey(x => x.Id); //设置主键
x.Property(c => c.Name).HasColumnType("nvarchar(10)").IsRequired();//设置字段类型
});
//设置Student对应的数据表
modelBuilder.Entity<Student>(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<Grade>(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.
查看生成的对应数据库表: