Reputation: 5595
I'm getting the Unable to create object context
error when using Entity framework in ASP.NET MVC.
Every time I POST
to the controller I'm not getting a response back. I tried going directly to the method of the controller /Data/GetAll
, and receive this error:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid. 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 originated in the code.
Exception Details:
System.ArgumentException:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
public class TrackItContextCreator {
public ObjectContext Create() {
ObjectContext context = new ObjectContext("name=TrackItDBEntities");
context.DefaultContainerName = "TrackItDBEntities";
return context;
}
}
<add name="TrackItDBEntities" connectionString="metadata=res://*
/EntityFramework.TrackItDBModel.csdl|res://*/EntityFramework.TrackItDBModel.ssdl|res:
//*/EntityFramework.TrackItDBModel.msl;provider=System.Data.SqlClient;provider
connection string="Data Source=host;User ID=myuseracc;Password=******;
MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
What could I be missing?
Upvotes: 4
Views: 9392
Reputation: 417
If you develop web app with class libarary you should add databases' connectionStrings to both of Web.Config and App.Config.
Upvotes: 0
Reputation: 122624
From the comments, I think I know what the problem is.
If the model is in a separate assembly, you basically have to make sure that the connection string is specified in the app.config
/web.config
of every assembly that uses the model. I'm guessing that you only have it in the model project.
See this question for another example, and this thread on MSDN. Both similar issues involving models being in separate projects/assemblies. I'm 99% sure that this is related to your issue.
Upvotes: 7