項目結構: 首先引用 Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.Design 增加類DesignTimeDbContextFactory QHContext 類 編輯Qh.Data項目的csproj 增加 <I ...
項目結構:
首先引用
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