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

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

问题描述

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



之后,它注意到我试图在我的代码中的三个特定行上使用LINQ to Entities对实体,这就是错误来源的地方。



在这些行上,有很简单的LINQ语句,如:

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

我已经尝试过枚举,作为列表,没关系...仍然出现错误。当我正在构建它时,当我正在编辑代码时,可能会发生错误。



我有连接字符串,名为NameOfEntities 在我的配置文件中,< connectionStrings> 标签之间,这个帖子建议



.edmx住在另一个项目(一个类库)在解决方案。正如上面的例子所示,这不应该是一个问题,在错误开始出现之前我一直在开发这个系统。



尽可能多的信息,这里是完整的错误。消息的格式是为了易读:


System.InvalidOperationException:在应用程序配置中找不到名为CnmcEntities的连接字符串文件。
在System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
在system.Data.Entity.Internal.LazyInternalContext.InitializeContext()
在System.Data.Entity.Internal在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`源,Expression`1 selector)
在NameOfProject.Model.LiveData..ctor()在c:\PlaceOfTheProject\NameOfProject\Model\LiveData.cs:line 19
在NameOfProject.ViewModel。 ChildView C中的Models.DataPointsViewModel..ctor(LiveDataViewModel thisMainView)在c:\PlaceOfTheProject\NameOfProject\ViewModel\LiveData\LiveDataViewModel\ChildViewModels\DataPointsViewModel.cs:line45在NameOfProject.ViewModel.LiveDataViewModel.InitializeData()在c :\PlaceOfProject\NameOfProject\ViewModel\LiveDataViewModel\LiveDataViewModel.cs:line 103


这是我有的没有敏感信息)在app.config中:



< connectionStrings>
< add name =NameOfEntitiesconnectionString =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:



作为一个热修复,而不是将异常写入消息框,我只是吞下了异常。对应用程序没有任何不良影响 - 它仍然访问数据库中的数据。我讨厌这个解决方案,但是从目前来看,似乎没有任何问题可以阻止这个例外,我现在就会保留这个。



有趣的是,我在违规代码的catch中设置了一个断点,并且在运行应用程序后还没有被击中几次。在关闭应用程序之后,似乎异常最常发生在资源释放中,所以也许调试器在遇到此代码时不再运行。无论如何,这确实是一个奇怪的事情。



我仍然可以接受任何具体的想法,如何解决问题,而不会吞咽例外或课程。 / p>

解决方案

这是一个库项目中的代码,如果是这样,您将需要在Web应用程序,控制台或Windows应用程序中检查连接字符串这是调用图书馆项目。


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

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.

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

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.

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

Here's what I have (sans sensitive info) in the 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:

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.

解决方案

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天全站免登陆