[英]Solution with pre Core Class Library & .net Core 2 MVC projects - web.config connectionstring not working
I have a solution containing many projects. 我有一个包含许多项目的解决方案。
One of the projects is a data access layer Class Library. 项目之一是数据访问层类库。 It uses this line for the connection string which it gets from the web.config of any other project that references it: 它使用此行作为连接字符串,它是从引用它的任何其他项目的web.config获取的:
private string Constr = ConfigurationManager.ConnectionStrings["DefaultConn"].ConnectionString;
I've added a web.config to a .net core 2 MVC app, but I get this error: 我已将web.config添加到.net core 2 MVC应用程序,但出现此错误:
FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=123456'. FileNotFoundException:无法加载文件或程序集'System.Configuration.ConfigurationManager,版本= 0.0.0.0,区域性=中性,PublicKeyToken = 123456'。 The system cannot find the file specified. 该系统找不到指定的文件。
I've also tried adding the connection string to the appsettings.json file - but it also doesn't help; 我也尝试将连接字符串添加到appsettings.json文件中-但这也无济于事。 the full contents of appsettings.json: appsettings.json的全部内容:
{
"ConnectionStrings": {
"DefaultConn": "Server=.;Database=MyDatabase;Trusted_Connection=true;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
I've found a few articles online dancing around it - but none that can fix this. 我在网上发现了几篇关于它的文章-但没有一篇可以解决此问题。
How to read web.config file in .Net Core app This one mentions a similar situation and a nuget package - but it doesn't solve the issue for the user. 如何在.Net Core应用程序中读取web.config文件这提到了类似的情况和nuget包-但它无法为用户解决问题。
How to include reference to assembly in ASP.NET Core project This one touches on it but applies to Hangfire only, not a Class Library using ConfigurationManager 如何在ASP.NET Core项目中包括对程序集的引用此内容涉及但仅适用于Hangfire,不适用于使用ConfigurationManager的类库
How can I do this? 我怎样才能做到这一点? Thanks. 谢谢。
I realise that ideally you'd use DI and appsettings.json to store a connection string in .net core MVC 2. 我意识到,理想情况下,您将使用DI和appsettings.json将连接字符串存储在.net核心MVC 2中。
However, regarding my specific question - passing a connectionstring to a Class Library that's already expecting something from the web.config - and to aid in transitioning to .net core - this other question was useful: How to read web.config file in .Net Core app 但是,关于我的特定问题-将连接字符串传递给已经期望来自web.config的类库-并帮助过渡到.net核心-这个另一个问题很有用: 如何在.Net中读取web.config文件核心应用
But no answer answered it - a comment did from Zuhair. 但是没有答案回答-Zuhair发表了评论。 Basically just rename web.config app.config and it works. 基本上只需重命名web.config app.config即可。 It works for the linked class library too. 它也适用于链接的类库。
Here is my app.config: 这是我的app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=.;Database=mydb;Trusted_Connection=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
You also need to install this NuGet package: 您还需要安装此NuGet软件包:
System.Configuration.ConfigurationManager
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.