2012-08-22 39 views
4

我已經將兩個新屬性添加到我的域模型類,並將兩個屬性相應地添加到數據表中。然後我試圖啓動我的MVC Web應用程序,並得到無法使EF Code First與手動更改的數據庫一起工作

The model backing the 'EFDbContext' context has changed since the database was created. 
Consider using Code First Migrations to update the database 
(http://go.microsoft.com/fwlink/?LinkId=238269). 

看了下面的帖子:

MVC3 and Code First Migrations

EF 4.3 Automatic Migrations Walkthrough

我通過軟件包管理器試圖Update-Database控制檯,但出現錯誤

Get-Package : Не удается найти параметр, соответствующий имени параметра "ProjectName". 
C:\Work\MVC\packages\EntityFramework.5.0.0\tools\EntityFramework.psm1:611 знак:40 
+  $package = Get-Package -ProjectName <<<< $project.FullName | ?{ $_.Id -eq 'EntityFramework' } 
+ CategoryInfo   : InvalidArgument: (:) [Get-Package], ParameterBindingException 
+ FullyQualifiedErrorId : NamedParameterNotFound,NuGet.PowerShell.Commands.GetPackageCommand 

The EntityFramework package is not installed on project 'Domain'. 

但是實體框架安裝在項目上。我將它從引用中刪除,刪除package.config併成功重新安裝EF。但Update-Database仍然返回相同的錯誤。 Update-Database -Config也可以做

我做錯了什麼?

編輯

非常感謝拉吉斯拉夫Mrnka,我會嘗試重組我的問題。據我手動更改我的數據表,我不希望使用遷移。但是,我現在怎樣才能使用手動編輯的域模型類和數據表使EF工作?

+1

你想使用遷移或者你只是想EF與您手動更改數據庫的工作嗎?遷移的重點是讓EF定義並更新您的數據庫。 –

+0

@LadislavMrnka我「*只是想EF與你手動更改的數據庫一起工作*」......因爲我是EF新手,我認爲遷移是我的問題提示......如果我錯了,請指導我正確的方式! – horgh

+0

有多種方法可以使用EF。您可以使用代碼優先映射並手動維護數據庫,也可以先使用代碼並讓EF維護數據庫 - 這就是遷移所做的事情。 –

回答

21

嘗試添加到您的應用程序的啓動(你可以把它App_Start):

Database.SetInitializer<EFDbContext>(null); 

應該關閉相關處理來自EF數據庫中的所有邏輯。您現在將完全負責保持您的數據庫與您的模型同步。

+0

現在它說*無效的列名*爲新列 – horgh

+0

同樣的錯誤顯示在SSMS中的智能感知在*選擇TOP 1000行*命令...雖然它的工作原理和返回行 – horgh

+0

所以你是否將這些列添加到數據庫或不? –

2

我有同樣的問題,這是我如何解決這個問題。

我使用sql命令刪除表__MigrationHistory並再次運行update-database -verbose。

顯然這個自動創建的表有些問題。

0

答案2正是需要的。儘管當我到達App_Start時,我意識到有4個配置文件,並沒有看到它們適合哪個。相反,我把它添加到我的EF數據庫上下文

namespace JobTrack.Concrete 
{ 
    public class EFDbContext : DbContext 
    { 
     //Set the entity framework database context to the connection name 
     //in the Webconfig file for our SQL Server data source QSJTDB1 
     public EFDbContext() : base("name=EFDbConnection") 
     {    
     } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     //Remove the tight dependency on the entity framework that 
     //wants to take control of the database. EF by nature wants 
     //to drive the database so that the database changes conform 
     //to the model changes in the application. This will remove the 
     //control from the EF and leave the changes to the database admin 
     //side so that it continues to be in sync with the model. 
     Database.SetInitializer<EFDbContext>(null); 

     //Remove the default pluaralization of model names 
     //This will allow us to work with database table names that are singular 
     base.OnModelCreating(modelBuilder); 
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();    
    } 

    //Allows for multiple entries of the class State to be used with 
    //interface objects such as IQueryTables for the State database table 
    public DbSet<State> State { get; set; } 

} 

}