locked
Entity Framework code first with existing (old) DB RRS feed

  • Question

  • User-143192194 posted

    [firstly, sorry for any mistakes in my English]

    considering this scenario :

    I was given an old sql server backup file (.bak) in order to create web application. backup file restored correctly to sql server 2014 and everything went OK with creation of EF classes , but fetching data from DB faces this error:

    An unhandled exception of type 'System.Data.Entity.Core.EntityCommandCompilationException' occurred in EntityFramework.SqlServer.dll

    If I utilize another database which was created in SQL server 2014 environment , everything would work fine. how can I fix it? thanks  in advance.

    using(newDatabase dbContext=new newDatabase())
                {
                    var list = dbContext.Operations.ToList();
                }// everything is OK
    
    using(backupDB dbCon=new backupDB())
                {
                    var list = dbCon.Roles.ToList();
                }//Error 

    Thursday, May 14, 2015 10:34 AM

Answers

  • User1711366110 posted

    I was given an old sql server backup file (.bak) in order to create web application. backup file restored correctly to sql server 2014 and everything went OK with creation of EF classes , but fetching data from DB faces this error

       As per this case, First of all you have to make sure that the bak file was not corrupted one. Remove lazy loading by altering the scaffolding of the EF classes/DbContext.

    then the following things you need to do before, during and after the re-import.

    1. You should remember/copy the name of the DbContext so you use the same name when you re-import. That way it will recompile properly without major name changes.

    2. Because you are using the same name as the existing DbContext you must delete the previous DbContext otherwise the re-importing process will fails. If its easier you can delete all the generated files as they are replaced anyway. That is why I suggest you put them in a separate directory with no other files added.

    3.When re-importing by default the process will add the connection string to your App.Config file again. I suggest you un-tick that otherwise you end up with lots of connection strings (minor point, but can be confusing).

    4.If you use source control (I really recommend you do) then a quick compare of the files to check what has changed is worthwhile.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 20, 2015 12:57 AM