rider下ef core迁移

新建数据库

sql 复制代码
create database mockstu

新建web项目

安装Microsoft.EntityFrameworkCore.SqlServer

设置连接字符串

新建model

cs 复制代码
using MockStuWeb.Models.EnumTypes;
using System.ComponentModel.DataAnnotations;

namespace MockStuWeb.Models
{
    /// <summary>
    /// 学生类
    /// </summary>
    public class Student
    {
        /// <summary>
        /// 唯一标识
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        [Display(Name = "名字")]
        [Required(ErrorMessage = "请输入名字"), MaxLength(50, ErrorMessage = "名字的长度不能超过50个字符")]
        public string? Name { get; set; }

        /// <summary>
        /// 主修课程
        /// </summary>
        [Required(ErrorMessage ="请选择主修课程")]
        [Display(Name = "主修课程")]
        public MajorEnum? Major { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        [Display(Name = "邮箱")]
        [RegularExpression(@"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$", ErrorMessage = "邮箱的格式不正确")]
        [Required(ErrorMessage = "请输入邮箱地址,它不能为空")]
        public string? Email { get; set; }
    }
}

新建dbContenxt

cs 复制代码
using Microsoft.EntityFrameworkCore;
using MockStuWeb.Models;

namespace MockStuWeb.Infrastructure;

public class AppDbContext:DbContext
{
    /// <summary>
    /// 构造函数注入
    /// </summary>
    /// <param name="options"></param>
    public AppDbContext(DbContextOptions<AppDbContext> options):base(options)
    {
    }
    
    public DbSet<Student> Students{get;set;}

}

program.cs中使用

cs 复制代码
builder.Services.AddDbContextPool<AppDbContext>(options =>
{
    options.UseSqlServer(configuration.GetConnectionString("MockStudentDBConnection"));
});

安装dotnet-ef

bash 复制代码
dotnet tool install --global dotnet-ef

创建迁移

bash 复制代码
cd .\MockStuWeb\
dotnet ef migrations add InitialCreate --namespace .\MockStuWeb.csproj --output-dir Migrations

安装Microsoft.EntityFrameworkCore.Tools,顺便升级一下sql server包

需要改代码


更新到数据库中

bash 复制代码
dotnet ef database update InitialCreate




启用种子数据

Infrastructure下新建ModelBuilderExtensions

cs 复制代码
using Microsoft.EntityFrameworkCore;
using MockStuWeb.Models;
using MockStuWeb.Models.EnumTypes;

namespace MockStuWeb.Infrastructure;

public static class ModelBuilderExtensions
{
    public static void Seed(this ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Student>().HasData(
            new Student
            {
                Id = 2,
                Name = "张三",
                Major = MajorEnum.ComputerScience,
                Email = "zhangsan@52abp.com"
            }
        );
    }
}

AppDbContext添加如下代码

cs 复制代码
/// <summary>
/// 模型创建的时候
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Seed();
}

添加迁移记录并更新

bash 复制代码
dotnet ef migrations add AlterStudentsSeedData --namespace .\MockStuWeb.csproj --output-dir Migrations
dotnet ef database update AlterStudentsSeedData



进行回退

bash 复制代码
dotnet ef migrations list
dotnet ef migrations remove -f

只能手动修改一下代码

建立表格的代码注释掉

单条数据也可以这么写


进行回退

bash 复制代码
dotnet ef migrations list
dotnet ef migrations remove -f
dotnet ef database update InitialCreate

把这次迁移的文件删除即可

参考

相关推荐
mudtools3 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫7 小时前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz1 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫1 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务2 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther2 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间2 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec2 天前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
Tiger_shl2 天前
【层面一】C#语言基础和核心语法-02(反射/委托/事件)
开发语言·c#