1.开发环境中修改模型,在DbContext中加入静态构造函数,并设置初始化模式:

staticDemoDbContext()

{

Database.SetInitializer(new DropCreateDatabaseIfModelChanges());

}

View Code

如果是Mysql,需要在DbContext加上 [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] 属性,否则操作数据库时会报错,更改结束后,注释掉就可以了

新增类时需要在DbContext添加该类的引用属性,如:新增Department类:

public DbSet Departments { get; set; }

View Code

2.生成环境中数据迁移

2.1 首先安装 Enitity Framework:Install-Package EntityFramework

2.2 修改DbContext静态构造函数:

staticDemoDbContext()

{

Database.SetInitializer(null);

}

View Code

2.3 在程序包管理器控制台,执行语句:

PM> Enable-Migrations -EnableAutomaticMigrations

这时会在工程下建立文件夹:Migrations 和 Configuration.cs 文件

internal sealed class Configuration : DbMigrationsConfiguration{publicConfiguration()

{

AutomaticMigrationsEnabled= true;

}protected override voidSeed(MvcDemo.DAL.DemoDbContext context)

{//This method will be called after migrating to the latest version.//You can use the DbSet.AddOrUpdate() helper extension method//to avoid creating duplicate seed data. E.g.//

//context.People.AddOrUpdate(//p => p.FullName,//new Person { FullName = "Andrew Peters" },//new Person { FullName = "Brice Lambson" },//new Person { FullName = "Rowan Miller" }//);//}

}

View Code

2.4 执行语句:(为测试效果,删除了Student类中的Email和Score属性)

PM> Add-Migration InitialCreate

此时会自动生成InitialCreate类:

usingSystem;usingSystem.Data.Entity.Migrations;public partial classInitialCreate : DbMigration

{public override voidUp()

{

DropColumn("dbo.Student", "Email");

DropColumn("dbo.Student", "Score");

}public override voidDown()

{

AddColumn("dbo.Student", "Score", c => c.Double(nullable: false));

AddColumn("dbo.Student", "Email", c => c.String(unicode: false));

}

}

View Code

2.5 执行语句:Update-Database -Verbose 更改即应用到数据库中

2.6 新增类(表)

2.6.1 新增类 City,并在DbContext类中引用实例

public classCity

{public string ID { get; set; }public string Name { get; set; }

}

View Code

2.6.2 执行语句

PM> Add-Migration AddCity

2.6.3 执行语句

PM> Update-Database -Verbose

查看数据,City表已经添加

3. 参考资料

4.源码

mvc模型中MySQL类_Mvc5 EF6 CodeFirst Mysql (二) 修改数据模型相关推荐

  1. 在ASP.NET MVC 模型中 选择最好的方法将多个model(数据模型)传递到视图

    在ASP.NET MVC 模型中 选择最好的方法将多个model(数据模型)传递到视图 前提介绍 这个文章我们要讨论,在ASP.NET MVC模型的项目中,怎么选择一个最有效的方式来将多个数据模型(m ...

  2. Mvc5 EF6 CodeFirst Mysql (一) 新建一个Mvc项目并使用EF连接到Mysql数据库

    1.新建Mvc5项目,更改身份验证为:不进行身份验证,由于使用到webapi,勾选webapi选项 2.安装Entity Framework 3.安装mysql connector 地址:http:/ ...

  3. EF6 Codefirst+MySql 数据库迁移

    简介 项目使用MSSql作为数据库,但是因为SQL服务器贵那么一点,并发连接差那么一点,要把数据迁移到MySQL,顺带迁移过程以及问题. 环境 · Visual Studio 2013 · MySQL ...

  4. asp.net mvc 注册中的邮箱激活功能实现(二)

    邮件发送功能封装 /// <summary>         /// 发送注册邮件         /// </summary>         /// <param n ...

  5. thinkphp项目mysql类关系_ThinkPHP数据库与模型

    数据库连接器与查询构造器 总结:同之前版本相比,ThinkPHP5的数据库操作对底层进行优化设计,对各种操作进行了高级封装.既可以直接使用连接器进行高效的原声查询,也可以使用封装好的查询构造器进行直观 ...

  6. Asp.Net MVC 模型(使用Entity Framework创建模型类)

    Asp.Net MVC 模型(使用Entity Framework创建模型类) 这篇教程的目的是解释在创建ASP.NET MVC应用程序时,如何使用Microsoft Entity Framework ...

  7. powerdesigner 同步mysql 报错_PowerDesigner实用技巧小结 及 导出word,想字段顺序跟模型中一致,如何设置...

    powerdesigner导出word,想字段顺序跟模型中一致,如何设置 右键List of columns of the table %PARENT% ,selection,define  sort ...

  8. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的连接恢复和命令拦截...

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第四篇:MVC程序中实体框架的连接恢复和 ...

  9. powerdesigner mysql 反引号_PowerDesigner实用技巧小结 及 导出word,想字段顺序跟模型中一致,如何设置...

    powerdesigner导出word,想字段顺序跟模型中一致,如何设置 右键List of columns of the table %PARENT% ,selection,define  sort ...

最新文章

  1. python在通信中的应用_基于Redis的进程间通信——在C++里使用python的深度学习模型...
  2. docker 安装redis
  3. (94)FPGA 两个触发器时序分析模型中,涉及到哪些参数?,面试必问(十八)(第19天)
  4. path的图片下拉效果
  5. 暴风影音硬件加速播放高清影片
  6. java学习之路 之 Java集合练习题
  7. (附源码)springboot投票系统的开发毕业设计261136
  8. 计算机网络——大数据、物联网
  9. 解决:ORA-06502: PL/SQL: numeric or value error: NULL index table key value
  10. factory(工厂) 模式简单示例
  11. 李飞飞计算机视觉笔记(1)--数据驱动的图像分类方式:K最近邻与线性分类器
  12. python之路_面向对象
  13. 行人重识别+人脸识别+商品识别(新商品库对比)
  14. redis热key卡槽节点分配算法
  15. 【Linux】删除旧版本内核
  16. 面试官:请你说一说vuex的五个属性,分别是什么,区别和用途说一下
  17. [转] 心中永远是晴天
  18. MAVEN dependencies与dependencyManagement的区别
  19. 前端js获取php后台变量
  20. Android综合教务管理系统

热门文章

  1. 基于PyTorch的Seq2Seq翻译模型详细注释介绍(一)
  2. c++ Factor泛型编程示例
  3. AICompiler动态shape编译框架
  4. MinkowskiEngine多GPU训练
  5. CodeGen标记循环
  6. 构建一个移动应用程序要花多少钱?
  7. Linux服务器上监控网络带宽的18个常用命令
  8. 关于python文件读写小结
  9. OverflowError: cannot fit ‘int‘ into an index-sized integer
  10. Android ProgressBar 加载中界面实现(loading 动画) 实现菊花的效果