4

I've added two new properties to my domain model class and two properties to a data table accordingly. Then I tried to launch my mvc web application and got

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).

Having read the following posts:

MVC3 and Code First Migrations

EF 4.3 Automatic Migrations Walkthrough

I tried to Update-Database through Package Manager Console, but got an error

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'.

But the Entityframework is installed on project Domain. I removed it from references, deleted package.config and sucessfully reinstalled EF. But Update-Database still returns same error. Update-Database -Config does as well

What am I doing wrong?

EDIT

Many thanks to Ladislav Mrnka, I'll try to rephrase my question. As far as I changed my data table manually, I am not expected to use migration. But how can I now make EF work with manually edited domain model class and data table?

Community
  • 1
  • 1
horgh
  • 17,918
  • 22
  • 68
  • 123
  • 1
    Do you want to use migrations or do you simply want EF to work with your manually changed database? The point of migrations is to let EF define and update your database. – Ladislav Mrnka Aug 22 '12 at 07:27
  • @LadislavMrnka I "*simply want EF to work with your manually changed database*"...as I am new to EF, I came to a thought that migration is the cue for my problem....if I am wrong, direct me the right way, please! – horgh Aug 22 '12 at 07:36
  • There are multiple approaches to use EF. You can use code first mapping and maintain database manually or you can use code first and let EF maintain the database - that is what migrations do. – Ladislav Mrnka Aug 22 '12 at 07:37
  • I did everything manually, but how can I make EF work now? – horgh Aug 22 '12 at 07:39

3 Answers3

23

Try to add this to startup of your application (you can put it to App_Start):

Database.SetInitializer<EFDbContext>(null);

It should turn off all logic related to handling the database from EF. You will now be fully responsible for keeping your database in sync with your model.

horgh
  • 17,918
  • 22
  • 68
  • 123
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Now it says *Invalid column name* for both new columns – horgh Aug 22 '12 at 07:54
  • Same error is shown by intellisense in SSMS in *Select TOP 1000 rows* command...though it works and returns rows – horgh Aug 22 '12 at 07:56
  • So did you add those column to database or not? – Ladislav Mrnka Aug 22 '12 at 08:01
  • Of course, I did! I added them with the help of SSMS...then wanted just to check it, and opened *Select TOP 1000 rows*..it returned rows..but in the query editor in the select statement intellisense underlined names of the added columns with red line, saying *Invalid column name* – horgh Aug 22 '12 at 08:05
  • I tried to replace them with another columns (with different names), but SSMS intellisense still says *Invalid column name...* – horgh Aug 22 '12 at 08:06
  • That is a bug in SSMS. It will stop underline them once you refresh the database or restart SSMS. Are those exceptions fired by your application describing columns with correct names? – Ladislav Mrnka Aug 22 '12 at 08:08
  • Yes, when I start my mvc application I get same messages *Invalid column name...* from mvc in browser when it tries to visualize those entities ina view – horgh Aug 22 '12 at 08:10
  • I restarted SSMS, recreated those columns...no red line in SSMS now....but still an error in mvc – horgh Aug 22 '12 at 08:13
  • You are either modifying incorrect database or you are not giving those columns correct name (the name from the exception) - I have just tried it myself and it works without any issues. – Ladislav Mrnka Aug 22 '12 at 08:19
  • That's it...the problem was in my Web.config...forgot to change it when moved my repository to Domain project...`Database.SetInitializer(null);` is just what I need...Thank's a lot!!! – horgh Aug 22 '12 at 08:19
  • I don't know how it worked at all having a reference to an unexisting repository...but after I added the code that you suggested, the issue was my Web.config – horgh Aug 22 '12 at 08:25
2

I had the same problem and this is how I fixed the issue.

I dropped table __MigrationHistory using sql command and run the update-database -verbose again.

Apparently something was wrong with this automatic created table.

Daniel
  • 3,322
  • 5
  • 30
  • 40
0

Answer 2 was exactly what was needed. Although when I got to the App_Start I realized that there were 4 configuration files and didn't see where this would fit in any of them. Instead I added it to my EF database context

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; } 

}

}

Brian Quinn
  • 57
  • 1
  • 4