[EF Core]数据迁移(二)
生活随笔
收集整理的這篇文章主要介紹了
[EF Core]数据迁移(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘要
在實際項目中,大多都需要對業務邏輯以及操作數據庫的邏輯進行分成操作,這個時候該如何進行數據的遷移呢?
步驟
上篇文章:EF Core數據遷移操作
比如,我們將數據上下文放在了Data層。
看一下BlogContext內容如下:
public class BlogContext : DbContext{public BlogContext(DbContextOptions options) : base(options){}public DbSet<User> Users { set; get; }}在appsetting中配置連接字符串
{"ConnectionStrings": {"DefaultConnection": "Server=localhost;database=MyBlogDb;uid=root;pwd=abcd;"},"Logging": {"IncludeScopes": false,"LogLevel": {"Default": "Warning"}} }在StartUp啟動類中,做如下操作:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Wolfy.Blog.Data;namespace Wolfy.Blog {public class Startup{public Startup(IConfiguration configuration){Configuration = configuration;}public IConfiguration Configuration { get; }// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceCollection services){string connStr = Configuration.GetConnectionString("DefaultConnection");services.AddDbContext<BlogContext>(options => options.UseMySQL(connStr,opt => opt.MigrationsAssembly("Wolfy.Blog")));services.AddMvc();}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();app.UseBrowserLink();}else{app.UseExceptionHandler("/Home/Error");}app.UseStaticFiles();app.UseMvc(routes =>{routes.MapRoute(name: "default",template: "{controller=Home}/{action=Index}/{id?}");});}} }
可以看到上面可以指定遷移程序集
//// 摘要:// Configures the assembly where migrations are maintained for this context.//// 參數:// assemblyName:// The name of the assembly.//// 返回結果:// The same builder instance so that multiple calls can be chained.public virtual TBuilder MigrationsAssembly([CanBeNullAttribute] string assemblyName);大概意思是配置數據遷移保持的程序集。
總結
一個簡單的用法,遇到了,就記錄一下,希望對你有所幫助
總結
以上是生活随笔為你收集整理的[EF Core]数据迁移(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redis数据结构详解之Hash(四)
- 下一篇: linux的nvme驱动需要关心的统计项