zoukankan      html  css  js  c++  java
  • .Net Core3.1使用EFCore进行数据迁移(Migration)

    开发环境

    编译器:VS2019
    

    运行环境

    DotNet Core SDK(3.1.8)
    

    SqlServer迁移方式

    依赖包

    Microsoft.EntityFrameworkCore
    Microsoft.EntityFrameworkCore.SqlServer
    

    使用迁移命令需要的依赖包

    Microsoft.EntityFrameworkCore.Tools
    EntityFramework
    Microsoft.EntityFrameworkCore.Design
    

    迁移命令

    1.创建第一个迁移
    Add-Migration InitialCreate   
    
    2.创建数据库和架构
    Update-Database 
    

    如果在实体中需要新增CreatedTimestamp 字段

    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime CreatedTimestamp { get; set; }
    }
    

    执行如下命令创建新迁移:

    Add-Migration AddBlogCreatedTimestamp
    
    Update-Database
    

    如果执行Update-Database异常需要删除上一个添加的迁移命令

    删除上一个添加的迁移命令
    Remove-Migration
    

    appsettings.json配置

    新增ConnectionString节点

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
      
      "ConnectionString": {
        "SqlServer": "server=.;database=NetCoreDemo;uid=sa;pwd=123"
      }
    }
    
    

    Startup.cs配置如下

    public void ConfigureServices(IServiceCollection services)
    {
        string constr = Configuration.GetSection("ConnectionString:SqlServer").Value;
        services.AddDbContext<MyDbContext>(options =>
            options.UseMySQL(connstr, m => m.MigrationsAssembly("User.API"));
    
        services.AddControllersWithViews();
    }
    

    MySql迁移方式

    依赖包

    Microsoft.EntityFrameworkCore
    Microsoft.EntityFrameworkCore.Design
    Microsoft.EntityFrameworkCore.Tools
    MySql.Data.EntityFrameworkCore
    

    appsettings.json配置

    新增ConnectionString节点

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
      "ConnectionString": {
        "MySql": "Data Source=localhost;port=3306;database=NetCoreDemo;User Id=root;Password=12345"
      }
    }
    
    
    

    Startup.cs配置如下

    public void ConfigureServices(IServiceCollection services)
    {
        var constr = Configuration.GetSection("ConnectionString:MySql").Value;
        services.AddDbContext<MyDbContext>(
              options.UseMySQL(connstr, m => m.MigrationsAssembly("User.API"));
              );
    
        services.AddControllersWithViews();
    }
    

    在依次执行上述迁移命令即可

    注意执行命令时必须默认项目必须选择继承了DbContext 的那个程序集

  • 相关阅读:
    js函数——倒计时模块+无缝滚动
    一步步编写avalon组件02:分页组件
    mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理
    只用css实现“每列四行,加载完一列后数据自动填充到下一列”的效果
    某考试 T1 arg
    vijos 2035 奇数偶数与绚丽多彩的数
    bzoj 5093: [Lydsy1711月赛]图的价值
    [HEOI2016/TJOI2016]求和
    [TJOI2015]概率论
    Codeforces 616 E Sum of Remainders
  • 原文地址:https://www.cnblogs.com/Java-125/p/13749243.html
Copyright © 2011-2022 走看看