c# - 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.
解决方案
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.
- Have you overridden the connection string in a different place ?
- Check your
ConfigureServices
method (or where you read the connection string) and put a debug point there to get the working connection string. - If you have any
Environment
specificappsetting
file and the connection string is written there also what you forgot to change.
推荐阅读
- python - 根据 Pandas 中每天的第一条记录创建数据框
- r - 将数据框列添加到列表列
- c++ - 共享模式在共享访问中是什么意思?
- android - Android - 我什么时候应该在 Strings.xml 资源文件中转义 unicode?
- linux - 在 github 的 windows 代理上运行 docker 镜像
- c# - 在 Bogus 库中为 Enum 生成假数据
- android - Capacitor v3:在安卓上打开时出现空白页
- javascript - Javascript - 表格内的按钮 -> 插入 TextContent
- python - 赋值前引用的局部变量“archivo”
- list - (wx)Maxima:枚举列表,使它们从 0 开始?