无法找到的app.config实体框架连接字符串 [英] Can't Find Entity Framework Connection String in app.config

查看:596
本文介绍了无法找到的app.config实体框架连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个捕获的异常写入异常到一个消息框。我一直运行到的异常说System.InvalidOperationException:没有名为'NameOfEntities连接字符串可以在应用程序配置文件中找到的。

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..."

在此之后,它指出,我想在我的code三个具体的线路使用LINQ到实体的实体,而这也正是该错误源自。

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.

在这些线路,有pretty的简单的LINQ的语句,如:

On these lines, there is pretty simple LINQ statements like:

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

我已经尝试过作为一个枚举,作为一个列表,没关系......仍然出现错误。当我运行它,当我构建它的时候,我只需要编辑code发生这种错误,和。

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.

我在我的配置文件命名为NameOfEntities连接字符串中,℃至的C​​onnectionStrings&GT; 标签,<一个href="http://stackoverflow.com/questions/12622408/no-connection-string-named-myentities-could-be-found-in-the-application-config">this后期建议。

I have the connection string named "NameOfEntities" in my config file, between the <connectionStrings> tags, which this post suggests.

的的.edmx生活在另一个项目(一个类库)中的溶液中。至于挂上面的例子说明,这不应该是一个问题,我一直在开发这个系统的长期的错误之前开始来了。

The .edmx lives in another project (a class library) in the solution. 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:没有名为'CnmcEntities连接字符串可以在应用程序配置文件中找到。   在System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()   在system.Data.Entity.Internal.LazyInternalContext.InitializeContext()   在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型的EntityType)   在System.Data.Data.Entity.Internal.Linq.InternalSet`1.Initialize()   在System.Data.Entity.Internal.Linq.Set`.get_InternalContext()   在System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()   在System.LInq.Queryable.Select [TSource,TResult](IQueryable`来源,例pression`1选择)   在NameOfProject.Model.LiveData..ctor()在C:\ PlaceOfTheProject \ NameOfProject \型号\ LiveData.cs:行19   在NameOfProject.ViewModel.ChildViewModels.DataPointsViewModel..ctor(LiveDataViewModel thisMainView)在c:\PlaceOfTheProject\NameOfProject\ViewModel\LiveData\LiveDataViewModel\ChildViewModels\DataPointsViewModel.cs:line45在NameOfProject.ViewModel.LiveDataViewModel.InitializeData()在C:\ PlaceOfProject \ NameOfProject \视图模型\ LiveDataViewModel \ LiveDataViewModel.cs:103行

"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:line45 at NameOfProject.ViewModel.LiveDataViewModel.InitializeData() in c:\PlaceOfProject\NameOfProject\ViewModel\LiveDataViewModel\LiveDataViewModel.cs:line 103"

下面是我有(没有敏感的信息)在App.config:

Here's what I have (sans sensitive info) in the app.config:

&LT;的ConnectionStrings&GT; &LT;添加名称=NameOfEntities connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider连接字符串=安培; QUOT;数据源= SERVERSOURCE;初始目录= NameOfInitialCatalog;坚持安全信息= TRUE;用户ID =用户ID,密码=密码; MultipleActiveResultSets = TRUE;应用=的EntityFramework和放大器; QUOT;的providerName =System.Data这。 EntityClient/&GT; &LT; /的ConnectionStrings&GT;

<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>

修改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.

有趣的是,我把一个破发点,在追赶的违规code,它已经没有被击中尚未运行的应用程序数次。这似乎是异常发生的最常见的资源释放关闭该应用程序后,也许调试器不再运行,当它击中这个code。无论哪种方式,这一直是一个奇怪的发生确实如此。

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.

推荐答案

这是code库中的项目,如果是这样,你需要检查连接字符串中的Web应用程序,控制台或Windows应用程序的调用库项目。

Is this code in a library project, if so, you will need check connection string in web application , console, or windows application which is calling the library project.

这篇关于无法找到的app.config实体框架连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆