Problems with Entity Framework Connection Strings

c# entity-framework sql

Question

I am making a little library(DLL) to manage users and their roles/privileges. The plan is to be able to add this dll to an MVC project and be able to manipulate users/roles/etc. All the data resides in a SQL db.

I am using entity framework for data access.

So when I initialize a new RoleManager(this is the name of the main class in the lib I'm making) I supply it with a connectionString like so:

RoleManager roleManager = new RoleManager(string connectionString);

Then inside the constructor I do this:

db = new RoleManagerEntities(connectionString); //This is the EntityFramework

And I am trying to supply this connection string (among many others)

"metadata=res://*/RoleManager.csdl|res://*/RoleManager.ssdl|res://*/RoleManager.msl;provider=System.Data.SqlClient;provider connection string='Data Source=localhost;Initial Catalog=Login;Integrated Security=True;Connection Timeout=60; multipleactiveresultsets=true'"

And I get the following error:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

This question is a result of having trying to instantiate the EF from my new project without supplying a connection string and without having anything inside my app config for it to default to. Too bad I can't delete it now.

1
8
3/18/2015 9:00:14 AM

Accepted Answer

Just copy the connection string information from your DLL config file to your executable config file.

8
10/18/2010 8:09:36 PM

Popular Answer

Basically you are trying to instantiate an ObjectContext via this ObjectContext Constructor (String) without passing the string parameter in its expected format and that's the problem.
Here is what you need to do:

1. First create an entry in your in your "test project" app.config because that is the place that the CLR is looking at to find the connection string at runtime.

<configuration>
  <connectionStrings>
    <add name="RoleManagerEntities" connectionString="metadata=res:///RoleManager.csdl|res:///RoleManager.ssdl|res://*/RoleManager.msl;provider=System.Data.SqlClient;provider connection string='Data Source=localhost;Initial Catalog=Login;Integrated Security=True;Connection Timeout=60; multipleactiveresultsets=true'" />
  </connectionStrings>
</configuration>

2. Now change the code to pass the connection string name instead of the actual connection string:

db = new RoleManagerEntities("name=RoleManagerEntities");


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow