Delete .mdf file from app_data causes exception cannot attach the file as database

前端 未结 6 1100
我寻月下人不归
我寻月下人不归 2020-12-07 11:20

I am building a web application using VS 2012 MVC4 code first. In order to recreate the .mdf file after I changed the model, I manually deleted the file from th

相关标签:
6条回答
  • 2020-12-07 12:07

    Try deleting it again from SQL Management Studio. It'll complain that there's no physical file any more but will remove from the object browser (You'll see after you refresh it)

    0 讨论(0)
  • 2020-12-07 12:11

    Use SQL Server Object Explorer to delete the database rather than just deleting the .mdf file in Solution Explorer. Otherwise the SQL Server instance still thinks the database name is used and the next time you try to attach a database to the same name it won't work. See the section on Initial Catalog in this MSDN page:

    http://msdn.microsoft.com/en-us/library/jj653752.aspx#initialcatalog

    0 讨论(0)
  • 2020-12-07 12:14

    exit localhost iis express and try again.

    0 讨论(0)
  • 2020-12-07 12:16

    I delete my DBContext class and re created it again in my MVC Project. Then .mdf file is re created in App_Data

    0 讨论(0)
  • 2020-12-07 12:20

    Remove this line from the connection string that should work ;)

    "AttachDbFilename=|DataDirectory|whateverdatabasename-20130917064511.mdf"

    0 讨论(0)
  • 2020-12-07 12:23

    That what fixed it for me, From Package Manager Console run these commands:

      sqllocaldb.exe stop v11.0
      sqllocaldb.exe delete v11.0
    

    And then:

      Update-Database
    
    0 讨论(0)
提交回复
热议问题