vs2017 EFCore 迁移数据库命令

发布日期:2018-02-22    浏览次数:1646

项目结构:

 

首先引用

   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.Model\Qh.Model.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="CZY.Framework">
      <HintPath>..\lib\CZY.Framework.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration">
      <HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration\2.0.0\lib\netstandard2.0\Microsoft.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

 

本文网址:https://www.wyxxw.cn/blog-detail-2-21-356.html

返回列表

非特殊说明,本文版权归原作者所有,转载请注明出处

提示:本站所有资源仅供学习与参考,请勿用于商业用途。图片来自互联网~如侵犯您的权益,请联系QQ:1067507709.

提示:转载请注明来自:https://www.cnblogs.com/XM-CHC/p/8397804.html 。 转载人:momo