Code First Migrations更新數據庫結構的具體步驟

Code First Migrations更新數據庫結構的具體步驟

作者寒羽楓(cityhunter172)

 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.SqlClientconnectionString=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.csAutomaticMigrationsEnabled默認爲 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很重要,記錄的是從創建數據庫開始的全部更新的記錄,所以在你沒有絕對把握的情況下千萬別動它。

            </div>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章