简体   繁体   English

'在应用程序配置文件中找不到名为'MyEntities'的连接字符串。'

[英]'No connection string named 'MyEntities' could be found in the application config file.'

I have a solution which has two projects, a .NET Web API and a .NET class library. I have a solution which has two projects, a .NET Web API and a .NET class library. I have used ADO.NET Entity Framework to generate the.edmx for my class library project.我已经使用 ADO.NET 实体框架为我的 class 库项目生成.edmx。

I have also referenced the class library project and entity framework in my web API, so I have no issue referencing the correct models etc. (when creating a controller).我还在我的 web API 中引用了 class 库项目和实体框架,所以我引用正确的模型等没有问题(创建控制器时)。 I have also ensured that entity framework is installed for both the Web API and the Class Library (using Nuget package manager). I have also ensured that entity framework is installed for both the Web API and the Class Library (using Nuget package manager).

However, I have a simple controller that should return all of the records in a specified table.但是,我有一个简单的 controller 应该返回指定表中的所有记录。 However, whenever this controller is called, I receive the following error:但是,每当调用此 controller 时,我都会收到以下错误:

'No connection string named 'VWRoodepoortEntities' could be found in the application config file.'

At first, my Web API had no .config file, so I added a web.config template and added the necessary connection string (the same connection string found within the app.config of the class library). At first, my Web API had no .config file, so I added a web.config template and added the necessary connection string (the same connection string found within the app.config of the class library). My web.config now looks like this:我的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>

However, despite adding the connection string in the application config file (as requested by the error), I still get the exact same error.但是,尽管在应用程序配置文件中添加了连接字符串(按照错误的要求),我仍然得到完全相同的错误。 I then set the Web API as my startup project, However, I still get the same error.然后我将 Web API 设置为我的启动项目,但是,我仍然遇到同样的错误。 Is there something wrong with my connection string?我的连接字符串有问题吗? Any help is appreciated.任何帮助表示赞赏。

EDIT:编辑:

I have also added my connection string to my appsettings.json file in my Web API.我还在我的appsettings.json API 中的 appsettings.json 文件中添加了我的连接字符串。 This file now looks as follows:该文件现在如下所示:

{
  "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;"
  }
}

check in your web.config the name of the connection string must be with "My Entities."检查您的 web.config 连接字符串的名称必须带有“我的实体”。

// Try this. // 尝试这个。
1)string apiURL = ConfigurationManager.AppSettings["MyEntities"].ToString(); 1)string apiURL = ConfigurationManager.AppSettings["MyEntities"].ToString();
2)public static string ConnectionString = string.Empty; 2)公共 static 字符串 ConnectionString = string.Empty;
ConnectionString = ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString; ConnectionString = ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString;

// App.config -- Change connection name from "VWRoodepoortEntities" to "MyEntities". // App.config -- 将连接名称从“VWRoodepoortEntities”更改为“MyEntities”。

<connectionStrings> <add name="MyEntities" connectionString="server=servername;port=3306;database=my_database;uid=root;password=*****;SslMode=none" /> </connectionStrings>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在应用程序配置文件中找不到名为“MyEntities”的连接字符串 - No connection string named 'MyEntities' could be found in the application config file 在配置文件中找不到连接字符串。 但它在那里 - No connection string could be found in the config file. But it is there 在应用程序配置文件中找不到名为“BACEntities”的连接字符串。” 当从 .NET 5 Api 调用 'n .NET 框架 4.7.2 库时 - No connection string named 'BACEntities' could be found in the application config file.” when calling 'n .NET Framwork 4.7.2 Library from .NET 5 Api 在应用程序配置文件中找不到名为“ CREntities”的连接字符串 - No connection string named 'CREntities' could be found in the application config file 在应用程序配置文件中找不到名为“thenameofmyconnection”的连接字符串 - No connection string named “thenameofmyconnection” could be found in the application config file 在应用程序配置文件中找不到名为“ KgSoftProContext”的连接字符串 - No connection string named 'KgSoftProContext' could be found in the application config file InvalidOperationException:在应用程序配置文件中找不到名为“PayMyRentEntities”的连接字符串 - InvalidOperationException: No connection string named 'PayMyRentEntities' could be found in the application config file 在应用程序配置文件中找不到已命名的连接字符串 - No connection string named could be found in the application config file 具有EF6的C#插件-在应用程序配置文件中找不到名为“实体”的连接字符串 - C# addin with EF6 - No connection string named 'Entities' could be found in the application config file 使用EF6的WCF - 在应用程序配置文件中找不到名为“Entities”的连接字符串,[I SET IT] - WCF with EF6 - No connection string named 'Entities' could be found in the application config file , [I SET IT]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM