4

I'm using Code First concept in Entity Framework and I'm constantly getting the following exception while starting application:

Cannot attach the file 'C:\Users\Admin\Documents\Visual Studio 2012\
Projects\Pro\Pro.Web\App_Data\Pro.mdf' as database 'Pro'.

I've put this in Global.asax.cs but also without success:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ProWebContext>());
        var ctx = new ProWebContext();
        ctx.Database.Initialize(true);

I've checked under App_Data directory and there no database created. Also, under Server Explorer there is nothing under Data Connections. Everything was working fine yesterday and today not working at all. I've tried to connect to LocalDB with SQL Server Management studio but it says it cannot connect to local database. Any ideas what could be a problem?

Thanks!

Cristiano
  • 3,099
  • 10
  • 45
  • 67

4 Answers4

5

I found the solution. With newest SQL Server Management studio there is no problem in connecting to the local database. Connection needs to be established like this:

enter image description here

After logging in we can still see old database present even if there is nothing under App_Data directory and under Data Connections in Server Explorer in Visual Studio. When we delete that database from SQL Server Management studio and start application again there will be no more errors while attaching database.

Cristiano
  • 3,099
  • 10
  • 45
  • 67
  • Interesting answer, but this I think there is a standard way to deal with this, see my answer. – Lzh Oct 20 '13 at 19:27
  • 1
    @Mzn your answer did not solve the problem for me. After trying it, I still had to find away to delete the database from memory (I had already deleted it from the file system). I do not have SSMS installed, but I was able to use the SQL Server Object Explorer in Visual Studio to find and delete the database reference that was still there. – Stafford Williams Jul 05 '15 at 15:32
  • @StaffordWilliams So the solution is still to remove the database to allow EF to recreate it. Cristiano's method is to do it manually, mine is to do it through the DB initializer. I don't know why using the Initializer does not work in your case. Is it that the account used by the framework does not have permissions to drop a database? What if you want to drop/recreate again, are you going to do it manually again? – Lzh Jul 05 '15 at 15:57
  • @Mzn yeah I'm not sure, guess I'll cross that bridge when/if I come to it. – Stafford Williams Jul 05 '15 at 16:05
1

I was facing the same error when I saw this but I couldn't delete the database using SQL Server Management Studio so I remembered IDatabaseInitializer.

Setting a database initializer for my Code First context solved the issue. In my case adding an initializer to always drop the DB worked (I added the code in the static constructor of my context, most people add it in Global.asax):

    static SomeDbContext()
    {
        System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways<SomeDbContext>());
    }

There are other initializers of course.

Lzh
  • 3,585
  • 1
  • 22
  • 36
1

I just got into the same problem, and yet the answers above helped, the solution was different.

In my case, I was reusing a project that has already created it´s database. And since the name in the config was the same, it was throwing the exception, missleading the real solution.

It worth checking the database is not created and is different than the ones you used before. Change the name and go ahead.

Ricker Silva
  • 1,137
  • 4
  • 17
  • 37
0

I deleted the mdf file manually so the code will recreate it again, and had the same problem. I solved it by running update-database command in the package manager console.

Ronen Festinger
  • 2,260
  • 1
  • 23
  • 32