Vs2017 EFCore database migration command, vs2017efcore

Source: Internet
Author: User

Vs2017 EFCore database migration command, vs2017efcore

Project Structure:

 

First reference

Microsoft. EntityFrameworkCore. Tools

Microsoft. EntityFrameworkCore. Design

 

Added the class 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 class

 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);        }    }

  

 

Added the csproj for editing the Qh. Data Project.

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

An error may occur when the Assembly cannot be found. You need to add the specified running version to the PropertyGroup node.

<RuntimeFrameworkVersion> 2.0.3 </RuntimeFrameworkVersion>

The final configuration file is as follows:

<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> <ProjectReference Include = ".. \ Qh. model \ Qh. model. csproj "/> </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> <DotNetCliToolReference Include = "Microsoft. entityFrameworkCore. tools. dotNet "Version =" 2.0.1 "/> </ItemGroup> <Folder Include =" Migrations \ "/> </ItemGroup> </Project>View Code

 

 

Cmd to QH. Data project folder

Generate migration commands

Dotnet ef migrations add initDB

Update to database

Dotnet ef database update

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.