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

当你的实体模型与数据库架构不一致时,引发以下错误:
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很重要,记录的是从创建数据库开始的全部更新的记录,所以在你没有绝对把握的情况下千万别动它。

Code First Migrations更新数据库结构的具体步骤的更多相关文章

  1. 转载Code First Migrations更新数据库架构的具体步骤

    [转载] Code First Migrations更新数据库结构的具体步骤 我对 CodeFirst 的理解,与之对应的有 ModelFirst与  DatabaseFirst ,三者各有千秋,依项 ...

  2. Code First Migrations更新数据库结构(数据迁移) 【转】

    注意:一旦正常后,每次数据库有变化,做如下两步: 1. Enable-Migrations 2.update-database 背景 code first起初当修改model后,要持久化至数据库中时, ...

  3. ASP.NET MVC4 新手入门教程特别篇之一----Code First Migrations更新数据库结构(数据迁移)修改Entity FrameWork 数据结构(不删除数据)

    背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的 ...

  4. Code First Migrations更新数据库结构(数据迁移)

    背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建 (DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们 ...

  5. (转)使用Migrations更新数据库结构(Code First )

    原文地址:http://blog.csdn.net/luoyeyu1989/article/details/8275237 背景 code first起初当修改model后,要持久化至数据库中时,总要 ...

  6. 使用Code first 进行更新数据库结构(数据迁移)

    CodeFirst 背景  code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会 ...

  7. Code First 下自动更新数据库结构(Automatic Migrations)

    示例 Web.config <?xml version="1.0" encoding="utf-8"?> <configuration> ...

  8. Entity Framework 6 Code First的简单使用和更新数据库结构

    一.安装Entity Framework 6 在项目中右击选择“管理NuGet程序包",联机搜索Entity Framework,点击安装 二.配置数据库连接 在App.config中加入数 ...

  9. Code First 更新数据库结构(简单实现方法:会删除原来的数据)

    之前在 http://www.cnblogs.com/mmcmmc/p/3833265.html 写到关于“Code First 更新数据库结构”的东西. 可是由于某种原因,新手们会出现各种问题,好了 ...

随机推荐

  1. 一次tomcat服务器被入侵解决办法

    突然tomcat目录下莫名其妙的多了几个war文件,里面内容只有一个index.jsp,打开控制台发现多了几个应用,我可以确定不是我部署上去的,顺着应用访问竟然看到了

  2. JavaSE复习_5 Eclipse的常见操作

    △用鼠标点击生成的java源文件,按下F4可以观察类的生成的层次结构 △window-showreview:可以显示命令窗口.   window-perspective-reset:可以将打乱的命令窗 ...

  3. 【Todo】LR-逻辑回归

    因为逻辑回归对于计算广告学非常重要.也是我们平时广告推荐.CTR预估最常用到的算法.所以单独开一篇文章讨论. 参考这篇文章:http://www.cnblogs.com/sparkwen/p/3441 ...

  4. 使用Ant打包工具 基本介绍

    由于使用java,javac,jar等工具进行编译打包,即繁琐低效又容易出错,因此Ant出现了. Ant的出现就是专门为了打包编译java代码的,使用之前得稍微学一下.Ant的运行起来主要是依靠配置文 ...

  5. 在map中根据value获取key

    原文:http://blog.csdn.net/mexican_jacky/article/details/51789548 //根据map的value获取map的key private static ...

  6. CentOS查看CPU、内存、网络流量和磁盘 I/O【转载,待整理】

    http://blog.csdn.net/zbyufei/article/details/6413273

  7. 转:UML类图几种关系的总结

    转自:http://www.open-open.com/lib/view/open1328059700311.html 在UML类图中,常见的有以下几种关系: 泛化(Generalization),  ...

  8. 关于Freelists和Freelist Groups的研究【转】

    一.       什么是freelists 本文在于探讨Freelists和Freelist Groups的作用,存取机制,争用诊断和优化方法,同时通过理论和测试来推翻一些存在了很久的错误观点.本文的 ...

  9. CSS3学习笔记之属性值

    font-family 设置文本的字体名称. font-style 设置文本样式. 取值 normal不使用斜体. italic使用斜体. oblique使用倾斜体. inherit从父元素继承. f ...

  10. PacBio软件总览 - 初级分析

    PacBio® RS Software Overview PacBio运行的整个流程是什么?每一步都用到了什么软件? PacBio软件套件 RS Remote:Design runs remotely ...