views:

368

answers:

1

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?

+1  A: 

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.

Aaronaught
@Downvoter: I edited to clarify, but if you down-voted because you think it's the wrong answer, I suggest you look here: http://stackoverflow.com/questions/2158853/entity-framework-the-underlying-provider-failed-on-connectionstring
Aaronaught
It shouldn't affect it, I have another project that uses " and it works. That was auto generated by the Entity Framework when I created it.
Matt
hmmm you were right I was missing it in the web.config of the main assembly, but none of the other ones had app.config/web.config files... the issue still isn't fixed. Any ideas?
Matt
+1. This is the correct answer. You must either put a correctly-named Entity Framework connection string in the config file of the *executing* assembly, or supply a connection string in code.
Craig Stuntz
@Matt: If you're still getting the exact same issue then there's probably something incorrect in the config. What happens if you try to get the connection string manually through `ConfigurationManager.ConnectionStrings["TrackItDBEntities"]`?
Aaronaught