Unable to create object context

I get a Unable to create object context error when using the Entity framework in ASP.NET MVC.

Background

Every time I POST to the controller, I do not get a response. I tried to go directly to the controller method /Data/GetAll and get this error:

Error

The specified named connection is either not found in the configuration, is not intended for use with EntityClient, or is invalid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it occurred in the code.

Exception Details: System.ArgumentException: specified named connection was either not found in the configuration and was not intended to be used with EntityClient, or is invalid.

A snippet of code that throws an exception:

 public class TrackItContextCreator { public ObjectContext Create() { ObjectContext context = new ObjectContext("name=TrackItDBEntities"); context.DefaultContainerName = "TrackItDBEntities"; return context; } } 

Web.config

 <add name="TrackItDBEntities" connectionString="metadata=res://* /EntityFramework.TrackItDBModel.csdl|res://*/EntityFramework.TrackItDBModel.ssdl|res: //*/EntityFramework.TrackItDBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=host;User ID=myuseracc;Password=******; MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> 

What can I lose?

+4
source share
1 answer

From the comments, I think I know what the problem is.

If the model is in a separate assembly, you must ensure that the connection string is specified in app.config / web.config each assembly that uses the model. I assume that you only have this in the model design.

See this question for another example, and this thread on MSDN . Both similar problems associated with models are in separate projects / assemblies. I am 99% sure that this is due to your problem.

+7
source

Source: https://habr.com/ru/post/1300824/


All Articles