vs2017 EFCore 迁移数据库命令

2018-02-01 12:08:19来源:cnblogs.com作者:厦门_成人点击

分享

项目结构:

首先引用

   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>

cmd到QH.Data项目文件夹

生成迁移指令

dotnet ef migrations add initDB

更新到数据库

dotnet ef database update

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台