【发布时间】:2022-01-11 12:16:37
【问题描述】:
我有一个解决方案,它有两个项目,一个 .NET Web API 和一个 .NET 类库。我已使用 ADO.NET Entity Framework 为我的类库项目生成 .edmx。
我还在我的 Web API 中引用了类库项目和实体框架,因此引用正确的模型等(在创建控制器时)没有问题。我还确保为 Web API 和类库安装了实体框架(使用 Nuget 包管理器)。
但是,我有一个简单的控制器,它应该返回指定表中的所有记录。但是,每当调用此控制器时,我都会收到以下错误:
'No connection string named 'VWRoodepoortEntities' could be found in the application config file.'
起初,我的 Web API 没有 .config 文件,所以我添加了一个 web.config 模板并添加了必要的连接字符串(在类库的 app.config 中找到的相同连接字符串)。我的web.config 现在看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="VWRoodepoortEntities" connectionString="metadata=res://*/VWRoodepoortModel.csdl|res://*/VWRoodepoortModel.ssdl|res://*/VWRoodepoortModel.msl;provider=System.Data.SqlClient;provider connection string='data source=.;initial catalog=VWRoodepoort;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework';" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
但是,尽管在应用程序配置文件中添加了连接字符串(按照错误的要求),我仍然得到完全相同的错误。然后我将 Web API 设置为我的启动项目,但是,我仍然得到同样的错误。我的连接字符串有问题吗?任何帮助表示赞赏。
编辑:
我还在我的 Web API 中将我的连接字符串添加到我的 appsettings.json 文件中。该文件现在如下所示:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"VWRoodepoortEntities": "metadata=res://*/VWRoodepoortModel.csdl|res://*/VWRoodepoortModel.ssdl|res://*/VWRoodepoortModel.msl;provider=System.Data.SqlClient;provider connection string='data source=.;initial catalog=VWRoodepoort;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework;"
}
}
【问题讨论】:
-
是的,我的项目(Web API)确实有一个
appsettings.json。我的 Web API 使用 .NET 5.0,类库使用 .NET Framework 4.7.2。
标签: c# .net entity-framework asp.net-web-api database-connection