首页 > 解决方案 > Why asp.net core mvc still connect to the same database even though I change the database name in the connection string?

问题描述

I export a database named "aspnet-SimpleBank-C2FC2762-4DD0-4E9A-90C2-C876AA53348C" as SimpleBankDatabase.bacpac using SQL Server Management Studio. Then import SimpleBankDatabase.bacpac into a database as SimpleBank. I change the connection string in my application to connect to SimpleBank but the application still connect to the old one because I try to add insert some data into the database through the application.

I try to copy the "SimpleBank" to the folder that stores "aspnet-SimpleBank-C2FC2762-4DD0-4E9A-90C2-C876AA53348C" but it still does not work.

The first connection string is commented.

    {
    "ConnectionStrings": {
    //"DefaultConnection": "Server= 
    (localdb)\\mssqllocaldb;Database=
    aspnet-SimpleBank-C2FC2762-4DD0-4E9A-90C2- C876AA53348C;
    Trusted_Connection=True;MultipleActiveResultSets=true"

    "DefaultConnection": "Server= 

   (localdb)\\mssqllocaldb;Database=SimpleBank;
   Trusted_Connection=True;
   MultipleActiveResultSets=true"
   },
   "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
    },
    "AllowedHosts": "*"
    }

The database name is completely different but the application still connect to the old database.

标签: c#jsonasp.net-core.net-coreasp.net-core-mvc

解决方案


The simple thing is that if you have changed the connection string to a different database and if the string is correctly written it will definitely work ( even if it is wrong the exception will be thrown ). Taking consideration that you have correctly commented the first connection string ( which is not shown correct from your post ) I will suggest to check the following.

  1. Have you overridden the connection string in a different place ?
  2. Check your ConfigureServicesmethod (or where you read the connection string) and put a debug point there to get the working connection string.
  3. If you have any Environment specific appsetting file and the connection string is written there also what you forgot to change.

推荐阅读