Cannot attach the file *.mdf as database

后端 未结 22 1861
猫巷女王i
猫巷女王i 2020-11-27 14:22

Basically I\'ve followed a tutorial and decided to delete the .mdf file afterwards.

Now whenever I try to run the application I get the following error

相关标签:
22条回答
  • 2020-11-27 14:40

    Recreate your database. Do not delete it and your app should continue to work.

    0 讨论(0)
  • 2020-11-27 14:42

    If you happen to apply migrations already this may fix it.

    Go to your Web.config and update your connection string based on your migration files.

    Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-GigHub-201802102244201.mdf;Initial Catalog=aspnet-GigHub-201802102244201;
    

    The datestrings should match the first numbers on your Migrations.

    0 讨论(0)
  • 2020-11-27 14:44

    Remove this line from the connection string that should do it ;) "AttachDbFilename=|DataDirectory|whateverurdatabasenameis-xxxxxxxxxx.mdf"

    0 讨论(0)
  • 2020-11-27 14:46
    1. From Package Manager Console run:

      sqllocaldb.exe stop v11.0

      sqllocaldb.exe delete v11.0

    2. Run your project

    3. Register a user
    0 讨论(0)
  • 2020-11-27 14:46

    You already have an old copy of that database installed in Server Explorer. So its a simple naming collision in the Server Object Explorer / SQL server. You likely created the same database Catalog Name already before you decided to move it to the Apps_Data folder. So that Database name already exists and just needs to be deleted.

    Just go into Visual Studio > View > SQL Server Object Explorer and delete the old database name and its connection. Retry your app again and it should install the .mdf file in App_Data and create the same exact database again in the Server Explorer.

    0 讨论(0)
  • 2020-11-27 14:47

    I think that for SQL Server Local Db you shouldn't use the Initial Catalog property. I suggest to use:

    <add name="DefaultConnection" 
         connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFoodDb.mdf" 
         providerName="System.Data.SqlClient" />
    

    I think that local db doesn't support multiple database on the same mdf file so specify an initial catalog is not supported (or not well supported and I have some strange errors).

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