1

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.

Community
  • 1
  • 1
jporcenaluk
  • 966
  • 10
  • 25
  • as long as the connection string exists within the current project's app.config you shouldn't get this issue. – Ric Nov 21 '13 at 19:09
  • I agree. But unfortunately, it's still coming up. I added what I have in the app.config to the post. – jporcenaluk Nov 21 '13 at 19:41
  • Not sure if you need those metadata in the connection string. – J.W. Nov 21 '13 at 21:09
  • I'll check out if I can remove the metadata and then get back to you. – jporcenaluk Nov 22 '13 at 15:40
  • let me know if it works or not. I never need that complicated part in my web.config – J.W. Nov 25 '13 at 01:02
  • I think the metadata stuff needs to stay in there for EF. Reference http://stackoverflow.com/questions/14213943/net-4-how-to-configure-edmx-file-in-other-assembly-in-web-config, http://blogs.teamb.com/craigstuntz/2010/08/13/38628/, and http://msdn.microsoft.com/en-us/library/cc716756.aspx. I wrapped the offending code in a try-catch and swallowed the exception. This is a terrible "solution" and I hate it, but the application is working the way it's supposed to. – jporcenaluk Nov 25 '13 at 13:31
  • It appears to me that you are trying to access the database context outside the scope. where do you put the line of code which blows. – J.W. Dec 04 '13 at 11:30
  • It's within the project that has the app.config that has the NameOfEntities connection string. This project and the class library project live within the same assembly. How do I determine scope? – jporcenaluk Dec 05 '13 at 14:32
  • you may try to access the dbcontext after it is closed based on your description. – J.W. Dec 22 '13 at 02:42

1 Answers1

0

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.

J.W.
  • 17,991
  • 7
  • 43
  • 76
  • The code that is throwing the error is in the project that is not the class library. The project that throws the error references the class library, and the .edmx file is in the class library. The Entity Framework connection string is not in the app.config of the class library, but is in the app.config of the project that references it. – jporcenaluk Nov 21 '13 at 20:22