简体   繁体   中英

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

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:line 45 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM