Matt
Matt

Reputation: 5595

Unable to create object context

I'm getting the Unable to create object context error when using Entity framework in ASP.NET MVC.

Background

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:

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.

Code Snippet that throws 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 could I be missing?

Upvotes: 4

Views: 9392

Answers (2)

Leo
Leo

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

Aaronaught
Aaronaught

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

Related Questions