http://blog.csdn.net/dj2008/article/details/23756895

http://blog.csdn.net/gentle_wolf/article/details/14004345

http://blog.csdn.net/cityhunter172/article/details/8062420

我对 CodeFirst 的理解,与之对应的有 ModelFirst与 DatabaseFirst ,三者各有千秋,依项目实际情况自行选择。

1、开发过程中先行设计数据库并依此在项目中生成 *.dbml 或是 *.edmx 文件的,就是DatabaseFirst;

2、开发时先建立空的 *.edmx 文件,由此文件生成数据库的,就是ModelFirst;

3、使用 System.Data.Entity. DbContext 与 System.Data.Entity. DbSet构建数据模型,没有可视化文件,只有实体类的,就是CodeFirst。

现在重点讲解在 CodeFirst 模式下,当你的实体类发生变化时,如何自动更新数据库以及遇到的各类问题。查阅相关资料,支持自动更新的变化有以下几种:

a、增加属性或者类

b、对属性和类重命名(想要使其正常工作,需要编写一些脚本)

c、对列(column)或者表(table)重命名,而不对属性或类重命名

d、删除属性

一、打开程序包管理器控制台

当你的实体模型与数据库架构不一致时,引发以下错误:
The model backingthe 'SchoolContext' context has changed since the database was created.Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)
百度搜索Code First Migrations,都说要执行命令 Update-Database ,在哪执行呢?继续找呀找,VS.NET的“程序包管理器控制台”出现了,但文章又没说在哪调出这个控制台。我也是初学者,找了半天终于知道它住哪了, “借问Console哪里有?牧童遥指视图中”。VS.NET →“视图”工具栏→其它窗口→程序包管理器控制台

二、安装EntityFramework

打开“程序包管理器控制台”,设置好包含实体模型的项目,执行 Update-Database如果你的项目只有一层,可能不会出现下列错误。我的测试项目把实体模型放在 Mvc4DAL,因此提示“未安装任何程序包。The EntityFramework package is not installed on project'Mvc4DAL'.”

1、右击Mvc4DAL项目→管理NuGet程序包

2、设置好程序包源

在界面中出现“未能解析此远程名称:’nuget.org’”字样,恭喜你,这步你不能跳过。点击左下方的“设置”按钮

右在设置窗口中新建一个程序包源,输入源的 URL “http://157.56.8.150/api/v2/” 勾选启用,点击“确定”

3、安装EntityFramework

选择刚刚新建的“程序包源”,在列表中找到EntityFramework,并安装

三、需要置好相关参数

1、设置好数据库连接字串

在项目中,找到 app.config(没有则在项目根目录手动新增,这里的设置只对本项目有效,不会影响到 Web 项目中的设置)。配置 <connectionStrings> 节点,新增 <addname="MyDBConnectString" providerName="System.Data.SqlClient"connectionString="datasource=.;initial catalog=MyDB;user id=sa;password=123456"/>,Mvc4DAL 用的是名称 MyDBConnectString 的连接字串

publicclassSchoolContext : DbContext

{

publicSchoolContext() : base("MyDBConnectString")

若没有设置好连接字串,或是字串设置有误,将出现如下提示:
Anerror occurred while getting provider information from the database. This canbe caused by Entity Framework using an incorrect connection string. Check theinner exceptions for details and ensure that the connection string is correct.

2、运行命令Enable-Migrations

出现如下提示时,你需要执行 Enable-Migrations:
Nomigrations configuration type was found in the assembly 'Mvc4DAL'. (In VisualStudio you can use the Enable-Migrations command from Package Manager Consoleto add a migrations configuration).

打开“程序包管理器控制台”,运行命令 Enable-Migrations ,Mvc4DAL项目中将出现Migrations文件夹与相应的文件 Configuration.cs

执行 Enable-Migrations 时可能会因为错误而打断,此时需要再次运行加参数的命令Enable-Migrations -Force:
Migrationshave already been enabled in project 'Mvc4DAL'. To overwrite the existingmigrations configuration, use the -Force parameter.

注意“Enable-Migrations”中间没有空格,“-Force”前面必须要有空格。

3、设置 AutomaticMigrationsEnabled为 true

打开Migrations文件夹中的 Configuration.cs,AutomaticMigrationsEnabled默认为 false 改为 true就哦了,否则将出现提示:
Unable to update database to match the current model because there arepending changes and automatic migration is disabled. Either write the pendingmodel changes to a code-based migration or enable automatic migration. SetDbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enableautomatic migration. You can use the Add-Migration command to write the pendingmodel changes to a code-based migration.

四、最后执行 Update-Database

上述步骤都设置好了,打开“程序包管理器控制台”,运行命令 Update-Database,没有出错就大功成。这里要注意的是,数据库中有个名称为dbo.__MigrationHistory 的Table很重要,记录的是从创建数据库开始的全部更新的记录,所以在你没有绝对把握的情况下千万别动它。

最新文章

  1. 浏览器调试js技巧总结
  2. java中的访问修饰符
  3. KVO/KVC 实现机理分析
  4. 状态压缩DP--Mondriaan&#39;s Dream
  5. 天使之城(codevs 2821)
  6. winform - BackgroundWorker
  7. 【待解决】编译V8引擎出错-snapshot.cc
  8. Bug记录:微博的Java SDK返回经纬度错误
  9. 【源码】canal和otter的高可靠性分析
  10. Android项目开发填坑记-so文件引发的攻坚战
  11. 购物车自己sql错误
  12. 深入JAVA注解(Annotation):自定义注解 (转)
  13. mysql索引优化比普通查询速度快多少
  14. POJ1180 Batch Scheduling -斜率优化DP
  15. Java基础加强之反射
  16. 96、facebook Fresco框架库源使用(转载)
  17. uva10689矩阵快速幂
  18. LR监控apache服务器
  19. RabbitMQ (一)
  20. Go语言(三)反射机制

热门文章

  1. 剑指offer-第五章优化时间和空间效率(从1到n的整数中1出现的次数)
  2. win7安装和配置IIS7
  3. 圆方树总结 [uoj30]Tourists
  4. Pod Installing openssl-ios-bitcode报错
  5. 不常用的linux命令
  6. MVC 控制器之间传值学习——session
  7. OBS第三方推流直播教程
  8. 【转】Jmeter项目测试
  9. 银行排队问题之单队列多窗口加VIP服务(30 分)
  10. mysql设计与开发