Question

I am following an MVC4 tutorial on Pluralsight, and all of my demo projects are source controlled through Visual Studio Online currently I am only testing locally (not deployed to Azure or anything). I am developing an ASP.NET MVC4 applications leveraging EF5, code first model. I enabled migrations, and the following class was created which I updated the seed method for and turned on automatic migrations.

internal sealed class Configuration : DbMigrationsConfiguration<MyProject.Models.MyProjectContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(MyProject.Models.MyProjectContext context)
    {
        context.MyCollection.AddOrUpdate(v => v.attr1,
            new MyProject() { attr1 = "MVC4", attr2 = 120},
            new MyProject() { attr1 = "LINQ", attr2 = 200});

        context.SaveChanges();
    }
}

I am using the default connection string to my localDb instance with the dynamic data directory (not hard-coding the path).

"AttachDBFilename=|DataDirectory|\MyProject.Models.MyProjectContext.mdf"

Everything was working as expected until I decided to change the physical directory of my solution. I changed the directory from the default C:/user/documents/visual studio 2013/projects folder over to another storage drive at D:/VisualStudio. I updated the mapping for my source control and pulled down the project to the new location.

However, when I run my project, I no longer have any data. In fact, I cannot run the update-database command without receiving the following error message:

Cannot attach the file 'D:\Visual Studio\MyProject\MyProject\App_Data\MyProject.Models.MyProjectContext.mdf' as database 'MyProject.Models.MyProjectContext'.

I attempted to turn off automatic migrations, re-run the "enable-migrations" command, and attempted hard-coding the path to store the .mdf file, but it can never "attach" to the database. I know that the database is no longer there, but I though if I ran the update-database -force command, if the system didn't find the database, it would create a new one.

Can someone assist on what I can do to regain my .mdf file in the App_Data folder?

Retaining existing data is a non-issue, I just need to have a new database to continue to work out of for the tutorial.

Was it helpful?

Solution

There are a number of options you can try out and you app should be able to run again.

Option 1

Rename your database name in the web.config file, then run Update-Database again.

Option 2

remove the following line from web.config file, then run Update-Database again.

AttachDbFilename=|DataDirectory|DataContext.mdf

Option 3

Since you have enabled migration, delete the database from Server Explorer, and from Object explorer and the .mdf file from app_data folder in your app. Then run Update-Database again.

I believe all those options should be able to work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top