当前位置: 首页 > news >正文

成都网站建设-中国互联上海网站建设专业公司

成都网站建设-中国互联,上海网站建设专业公司,wordpress 直达链接,在线软件网站建设一.前言 我们在使用EF进行开发的时候#xff0c;肯定会遇到将迁移更新到生产数据库这个问题#xff0c;前面写了一篇文章介绍了Entity Framework Core 2.0的入门使用#xff0c;这里面介绍了使用命令生成迁移所需的SQL#xff0c;然后更新到生产数据库的方法。这里还有另一… 一.前言 我们在使用EF进行开发的时候肯定会遇到将迁移更新到生产数据库这个问题前面写了一篇文章介绍了Entity Framework Core 2.0的入门使用这里面介绍了使用命令生成迁移所需的SQL然后更新到生产数据库的方法。这里还有另一种方法就是利用EF Core自身所提供的方法来进行迁移。 二.API说明 这些方法都是DatabaseFacade的扩展方法我们常使用的DbContext.Database就是DatabaseFacade类型。 GetMigrations 获取所有迁移 /// summary/// Gets all the migrations that are defined in the configured migrations assembly./// /summarypublic static IEnumerablestring GetMigrations([NotNull] this DatabaseFacade databaseFacade) GetPendingMigrations 获取待迁移列表 /// summary///     Gets all migrations that are defined in the assembly but havent been applied to the target database./// /summarypublic static IEnumerablestring GetPendingMigrations([NotNull] this DatabaseFacade databaseFacade) GetAppliedMigrations 获取执行了迁移的列表 /// summary///     Gets all migrations that have been applied to the target database./// /summarypublic static IEnumerablestring GetAppliedMigrations([NotNull] this DatabaseFacade databaseFacade) Migrate 执行迁移 /// summary///     para///         Applies any pending migrations for the context to the database. Will create the database///         if it does not already exist.///     /para///     para///         Note that this API is mutually exclusive with DbContext.Database.EnsureCreated(). EnsureCreated does not use migrations ///         to create the database and therefore the database that is created cannot be later updated using migrations.///     /para/// /summary/// param namedatabaseFacade The see crefT:Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade / for the context. /parampublic static void Migrate([NotNull] this DatabaseFacade databaseFacade) 三.实现自动迁移 我们可以利用上面的方法让程序在启动的时候检查是否有待迁移如果有那么执行迁移。这里以一个.NET Core 控制台应用程序作为示例 1.定义一个检查迁移的方法 /// summary/// 检查迁移/// /summary/// param namedb/paramstatic void CheckMigrations(BloggingContext db){Console.WriteLine(Check Migrations);    //判断是否有待迁移if (db.Database.GetPendingMigrations().Any()){Console.WriteLine(Migrating...);        //执行迁移db.Database.Migrate();Console.WriteLine(Migrated);}Console.WriteLine(Check Migrations Coomplete!); } 2.在程序启动时调用 static void Main(string[] args){    using (var db new BloggingContext()){        //检查迁移CheckMigrations(db);...} } 运行 四.制作一个单独的迁移工具 上面的方法需要我们每次在应用程序启动的时候都去检查迁移我们也可以单独制作一个控制台程序来进行迁移的更新这样只要在更新迁移的时候放到服务器上执行一下就行 了。 我们在实际使用中建议将EntityFrameWork Core单独作为一个项目 代码如下 static void Main(string[] args){Console.WriteLine(Entity Framework Core Migrate Start !);Console.WriteLine(Get Pending Migrations...);        using (var db new BloggingContext()){        //获取所有待迁移Console.WriteLine($Pending Migrations\n{string.Join(\n, db.Database.GetPendingMigrations().ToArray())});Console.WriteLine(Do you want to continue?(Y/N));         if (Console.ReadLine().Trim().ToLower() n){            return;}Console.WriteLine(Migrating...);               try{            //执行迁移db.Database.Migrate();}        catch (Exception e){Console.WriteLine(e);                        throw;}}Console.WriteLine(Entity Framework Core Migrate Complete !);Console.WriteLine(Press any key to exit !);Console.ReadKey(); } 执行效果 本文Demo:https://github.com/stulzq/EntityFramework-Core-Migrator .NET Core 交流群4656606 原文地址http://www.cnblogs.com/stulzq/p/7729380.html .NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注
http://www.zqtcl.cn/news/662323/

相关文章:

  • 软件网站是怎么做的帮别人做网站赚多少钱
  • 纯静态网站 搜索功能佛山网站建设 奇锐科技
  • 四川省建设厅官方网站联系电话自己网站做虚拟币违法吗
  • 同城招聘网站自助建站2014 网站建设
  • 个人网站空间大小江油官方网站建设
  • 怎样建网站做什么网站能吸引流量
  • 做vi设计的网站网络营销推广思路
  • 简述网站设计流程沁水做网站
  • 南京公司网站建设怎么收费获奖网页设计
  • 网站域名试用期水墨风格网站源码
  • 长沙网站开长沙手机网站建设哪些内容
  • 网站建设算固定资产吗做泵阀生意到哪个网站
  • 佛山网站建设定制杭州人防质监站网址
  • 什么网站可以做微官网定制小程序制作一个需要多少钱
  • 扒下来的网站怎么做修改什么样是权网站重高的
  • 淘宝客做网站链接潍坊网站建设wfzhy
  • 怎样做二维码链接到网站上做的比较好的美食网站有哪些
  • 自动化科技产品网站建设响应式博客wordpress
  • 个人建站如何赚钱男人的好看网
  • 门户网站建设管理工作作一手房用什么做网站
  • 网站建设优化服务案例三合一网站程序
  • 网站长尾词关于制作网站的方案
  • 做二手衣服的网站有哪些wordpress单本小说采集
  • 曲靖市建设局网站品牌营销咨询公司是做什么的
  • wordpress网站统计代码放哪个文件putty搭建wordpress
  • 桦南县建设局网站天坛装修公司口碑怎么样
  • 网站的建设求职简历网站开发与维护价格
  • 网站空间备份站长网站优点
  • 房产网站做那个比较好网页设计属于前端吗
  • 衡水企业网站建设费用html5网页设计教程