简体   繁体   English

在app.config中找不到实体框架连接字符串

[英]Can't Find Entity Framework Connection String in app.config

I have a catch for exceptions that writes the exception to a message box. 我有一个异常捕获,将异常写入消息框。 The exception I keep running into says "System.InvalidOperationException: No connection string named 'NameOfEntities' could be found in the application config file. at..." 我一直遇到的异常提示“ System.InvalidOperationException:在应用程序配置文件中找不到名为'NameOfEntities'的连接字符串。在...”

Following that, it notes that I'm trying to use LINQ to Entities on the entity on three specific lines in my code and that's where the error is originating from. 之后,它指出我正在尝试在代码中的三个特定行上使用LINQ to Entities on theEntity on theEntity,这就是错误的出处。

On these lines, there is pretty simple LINQ statements like: 在这些行上,有非常简单的LINQ语句,例如:

IEnumerable<EntityObject> item = (from d in entityVariable.EntityObjects select d).AsEnumerable<EntityObject>();

I have tried it as an enumerable, as a list, it doesn't matter...still the error appears. 我已经尝试过将其作为一个可枚举的列表,但没有关系...仍然出现错误。 The error can occur when I'm running it, when I'm building it, and when I'm just editing code. 当我运行它,构建它以及编辑代码时,可能会发生该错误。

I have the connection string named "NameOfEntities" in my config file, between the <connectionStrings> tags, which this post suggests . 我的配置文件中的<connectionStrings>标记之间有一个名为“ NameOfEntities”的连接字符串, 本文对此建议

The .edmx lives in another project (a class library) in the solution. .edmx位于解决方案中的另一个项目(类库)中。 As the example linked to above illustrates, this shouldn't be a problem, and I've been developing with this system long before the error starting coming up. 如上面链接的示例所示,这应该不成问题,并且在出现错误之前,我一直在使用此系统进行开发。

For as much info as possible, here's the full error. 有关尽可能多的信息,这是完整的错误。 The formatting of the message is for legibility: 邮件的格式易于辨认:

"System.InvalidOperationException: No connection string named 'CnmcEntities' could be found in the application config file. at System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel() at system.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.Set`.get_InternalContext() at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() at System.LInq.Queryable.Select[TSource,TResult](IQueryable` source, Expression`1 selector) at NameOfProject.Model.LiveData..ctor() in c:\\PlaceOfTheProject\\NameOfProject\\Model\\LiveData.cs:line 19 at NameOfProject.ViewModel.ChildViewModels.DataPointsViewModel..ctor(LiveDataViewModel thisMainView) in c:\\PlaceOfTheProject\\NameOfProject\\ViewModel\\LiveData\\LiveDataViewModel\\ChildViewModels\\DataPointsViewModel.cs:line “ System.InvalidOperationException:在应用程序配置文件中找不到名为'CnmcEntities'的连接字符串。位于System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()的system.Data.Entity.Internal.LazyInternalContext.InitializeContext()的位置System.Data.Data.Entity.Internal.Linq.InternalSet的System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(TypeEntityType)在System.Data.Entity.Internal.Linq.Set`.get_InternalContext处的1.Initialize() ()在System.LInq.Queryable.Select的System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()处,在NameOfProject中选择[TSource,TResult](IQueryable的源,Expression` 1选择器)。 c:\\ PlaceOfTheProject \\ NameOfProject \\ Model \\ LiveData.cs中的Model.LiveData.ctor():位于c:\\ PlaceOfTheProject \\ NameOfProject \\ ViewModel \\ LiveData中NameOfProject.ViewModel.ChildViewModels.DataPointsViewModel..ctor(LiveDataViewModel thisMainView) \\ LiveDataViewModel \\ ChildViewModels \\ DataPointsViewModel.cs:行 45 at NameOfProject.ViewModel.LiveDataViewModel.InitializeData() in c:\\PlaceOfProject\\NameOfProject\\ViewModel\\LiveDataViewModel\\LiveDataViewModel.cs:line 103" 45,位于c:\\ PlaceOfProject \\ NameOfProject \\ ViewModel \\ LiveDataViewModel \\ LiveDataViewModel.cs:第103行中的NameOfProject.ViewModel.LiveDataViewModel.InitializeData()

Here's what I have (sans sensitive info) in the app.config: 这是我在app.config中拥有的(无敏感信息):

<connectionStrings> <add name="NameOfEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SERVERSOURCE;initial catalog=NameOfInitialCatalog;persist security info=True;user id=UserId;password=Password;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings>

EDIT 1: 编辑1:

As a "hot fix," instead of writing the exception to a message box, I am just swallowing the exception. 作为“修补程序”,我只是吞下了该异常,而不是将异常写入消息框。 There has been no ill effects on the application - it still accesses the data from the database. 对应用程序没有不良影响-它仍然从数据库访问数据。 I hate this "solution," but as there seem to be no problems so far from swallowing the exception, I will keep it for now. 我讨厌这种“解决方案”,但是到目前为止,吞并该异常似乎没有任何问题,所以我暂时保留它。

Interestingly, I put a break point in the catch for the offending code, and it has not been hit yet after running the application several times. 有趣的是,我在有问题的代码中添加了一个断点,并且在多次运行应用程序后仍未找到该断点。 It seems like the exception was occurring most often on release of resources after closing the app, so maybe the debugger is no longer running when it hits this code. 关闭应用程序后,似乎最经常发生在资源释放上的异常,因此调试器在碰到此代码时可能不再运行。 Either way, this has been a strange occurrence indeed. 无论哪种方式,这的确是一种奇怪的情况。

I'm still open to any concrete ideas on how to fix the problem without swallowing the exception, or course. 我仍然对如何解决问题而不吞下异常或过程的任何具体想法持开放态度。

此代码是否在库项目中,如果是,则需要在调用库项目的Web应用程序,控制台或Windows应用程序中检查连接字符串。

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

相关问题 App.Config中的实体框架连接字符串 - Entity Framework Connection String in App.Config 实体框架未在app.config中使用连接字符串 - Entity Framework not using connection string in app.config 在Entity Framework的app.config中更改连接字符串 - Changing connection string in app.config for Entity Framework 实体框架迁移无法在App.config文件中找到连接字符串 - Entity Framework Migration cant find connection string in App.config file app.config和用于远程连接的实体框架 - app.config and Entity Framework for remote connection 实体框架无法在Web.config中找到连接字符串 - Entity Framework Can't Find Connection String in Web.config 实体框架(代码优先到现有数据库)-在运行时覆盖app.config中的连接字符串 - Entity Framework(Code First to an existing database) - overwrite a connection string in app.config at runtime 强制实体框架和ASP.MVC使用另一个程序集的app.config中的连接字符串 - Forcing Entity Framework and ASP.MVC to use the connection string from app.config of another assembly 实体框架抛出无效操作异常 - 找不到app.config中的连接字符串 - Entity framework throwing invalid operation exception - Connection string in app.config not found App.Config 连接字符串 - App.Config Connection String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM