Cannot attach the file *.mdf as database

后端 未结 22 1863
猫巷女王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:54

    Ran into this issue. Caused in my case by deleting the .mdf while iispexress was still running and therefor still using the DB. Right click on iisexpress in system tray and click exit THEN delete the MDF to prevent this error from actually occurring.

    To fix this error simply within VS right click the App-Data folder add new item > SQL Server Database. Name: [use the database name provided by the update-database error] Click Add.

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

    As per @davide-icardi, remove the "Initial Catalog=xxx;" from web.config, but also check for your azure publish profile file to remove it from here too:
    [YourAspNetProject path]\Properties\PublishProfiles[YourAspNetProjectName].pubxml

    <PublishDatabaseSettings>
      <Objects xmlns="">
        <ObjectGroup Name="YourAspNetProjectName" Order="1" Enabled="True">
          <Destination Path="Data Source=AzureDataBaseServer;Initial Catalog=azureDatabase_db;User ID=AzureUser_db_sa@AzureDataBaseServer;Password=test" />
          <Object Type="DbCodeFirst">
            <Source Path="DBMigration" DbContext="YourAspNetProjectName.Models.ApplicationDbContext, YourAspNetProjectName" MigrationConfiguration="YourAspNetProjectName.Migrations.Configuration, YourAspNetProjectName" Origin="Configuration" />
          </Object>
        </ObjectGroup>
      </Objects>
    </PublishDatabaseSettings>
    
    0 讨论(0)
  • 2020-11-27 14:56

    "Cannot attach the file 'C:\Github\TestService\TestService\App_data\TestService.mdf" as database 'TestService'

    When you meet the above error message, Please do the following steps.

    1. Open SQL Server Object Explorer
    2. Click refresh button.
    3. Expand (localdb)\MSSQLLocalDB(SQL Server 12.x.xxxxx - xxxxx\xxxx)
    4. Expand Database
    5. Please remove existed same name database
    6. Click right button and then delete
    7. Go back to your Package Manage Console
    8. Update-Database
    0 讨论(0)
  • 2020-11-27 14:56

    I have faced the same issue. The following steps in VS 2013 solved the problem for me:

    1. In Server Explorer add new Connect to Database
    2. Select Microsoft SQL Server Database File as Data source
    3. Choose database filename as it should be in according to connection string in your web.config
    4. New database file was created and two database connections was appeared in Server Explorer: "MyDatabaseName" and "MyDatabaseName (MyProjectName)"
    5. Delete one connection (I've deleted "MyDatabaseName")
    0 讨论(0)
提交回复
热议问题