zoukankan      html  css  js  c++  java
  • vs2017 EFCore 迁移数据库命令

    项目结构:

    首先引用

       Microsoft.EntityFrameworkCore.Tools

      Microsoft.EntityFrameworkCore.Design

    增加类DesignTimeDbContextFactory

       public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<QHContext>
        {
            public QHContext CreateDbContext(string[] args)
            {
    
                var builder = new DbContextOptionsBuilder();
                //builder.UseSqlServer("Server=(localdb)MSSQLLocalDB;Integrated Security=true;Initial Catalog=Light;");
                builder.UseSqlServer("Server=***;Database=**;User ID=**;Password=**;Trusted_Connection=false;Connect Timeout=120;MultipleActiveResultSets=True;");
                return new QHContext(builder.Options);
            }
        }
    

    QHContext 类

     public class QHContext : DbContext
        {
    
            public QHContext(DbContextOptions options) : base(options)
            {
    
            }
    
            public DbSet<Customer> Customers { get; set; }
            public DbSet<Order> Orders { get; set; }
            public DbSet<OrderItem> OrderItems { get; set; }
            public DbSet<Product> Products { get; set; }
            public DbSet<ProductItem> ProductItems { get; set; }
    
            public DbSet<User> Users { get; set; }
    
            public DbSet<BuckleRecord> BuckleRecords { get; set; }
            public DbSet<CustomerRelation> CustomerRelations { get; set; }
            public DbSet<OrderLog> OrderLogs { get; set; }
            public DbSet<RebateRecord> RebateRecords { get; set; }
            public DbSet<Recharge> Recharges { get; set; }
            public DbSet<Withdraw> Withdraws { get; set; }
    
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                //var builder = new ConfigurationBuilder()
                //            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
    
                //var configuration = builder.Build();
    
                //string connectionString = configuration.GetConnectionString("MyConnection");
    
                //optionsBuilder.UseMySQL(connectionString);
            }
    
            protected override void OnModelCreating(ModelBuilder builder)
            {
               
                //builder.Entity<Customer>().HasKey(m => m.CustomerId);
                builder.Entity<Customer>().ToTable("Customer");
                builder.Entity<BuckleRecord>().ToTable("BuckleRecord");
                builder.Entity<CustomerRelation>().ToTable("CustomerRelation");
                builder.Entity<Order>().ToTable("Order");
                builder.Entity<OrderItem>().ToTable("OrderItem");
                builder.Entity<OrderLog>().ToTable("OrderLog");
                builder.Entity<Product>().ToTable("Product");
                builder.Entity<ProductItem>().ToTable("ProductItem");
                builder.Entity<RebateRecord>().ToTable("RebateRecord");
                builder.Entity<Recharge>().ToTable("Recharge");
                builder.Entity<User>().ToTable("User");
                builder.Entity<Withdraw>().ToTable("Withdraw");
    
                base.OnModelCreating(builder);
            }
        }
    

      

     

    编辑Qh.Data项目的csproj 增加

    <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
    </ItemGroup>

    有可能报错找不到程序集,需要再PropertyGroup节点添加指定运行版本

    <RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion>

    最终的配置文件如下:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="DapperExtensions" Version="1.6.3" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.1" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" />
      </ItemGroup>
    
      <ItemGroup>
        <ProjectReference Include="..Qh.ModelQh.Model.csproj" />
      </ItemGroup>
    
      <ItemGroup>
        <Reference Include="CZY.Framework">
          <HintPath>..libCZY.Framework.dll</HintPath>
        </Reference>
        <Reference Include="Microsoft.Extensions.Configuration">
          <HintPath>C:Program FilesdotnetsdkNuGetFallbackFoldermicrosoft.extensions.configuration2.0.0lib
    etstandard2.0Microsoft.Extensions.Configuration.dll</HintPath>
        </Reference>
      </ItemGroup>
    <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
    </ItemGroup>
    <ItemGroup>
      <Folder Include="Migrations" />
    </ItemGroup>
    </Project>
    View Code

    cmd到QH.Data项目文件夹

    生成迁移指令

    dotnet ef migrations add initDB

    更新到数据库

    dotnet ef database update

    如果:执行命令总是提示 未找到与命令“dotnet-ef”匹配的可执行文件

    增加节点:

    <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
    </ItemGroup>
  • 相关阅读:
    css中的display以及position属性
    有人退税近4000元!个税年度汇算开始了,看看你能退多少?
    多维竞争
    阿里智能化接口测试平台--暴雪
    盗版网课有多猖狂?原价上万,只卖5元
    测试找BUG总结
    女程序员做了个梦。。。
    混沌的市场里,怎么一眼识别出「好房子」
    知乎扎心高赞:30岁还没有走到管理岗的人,后来怎么样了?
    自动化是在敏捷中提供连续测试的唯一方法
  • 原文地址:https://www.cnblogs.com/XM-CHC/p/8397804.html
Copyright © 2011-2022 走看看