ASP.NET MVC4 Code First - 'Cannot attach the file as database' exception

后端 未结 4 910
独厮守ぢ
独厮守ぢ 2020-12-21 03:58

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:         


        
4条回答
  •  眼角桃花
    2020-12-21 04:28

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

    There are other initializers of course.

提交回复
热议问题