8

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.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Jason
  • 11,435
  • 24
  • 77
  • 131

4 Answers4

8

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

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • I did. And this is what happens. "The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid." – Jason Oct 18 '10 at 20:14
6

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");
Morteza Manavi
  • 33,026
  • 6
  • 100
  • 83
0

The constructor might be looking for a connection string in the connectionStrings setting of your web.config with the name that you pass it as the parameter.

So if you call:

db = new RoleManagerEntities("Foobar");

It is looking for:

I'm not positive that this is the solution but that's what the error message seems to indicate.

Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • "Format of the initialization string does not conform to specification starting at index 0." Error – Jason Oct 18 '10 at 20:05
0

I am not an expert on EF, but I don't think that connection string is valid. Try:

metadata=res://*;provider=System.Data.SqlClient;provider connection string='Data Source=localhost;Initial Catalog=Login;Integrated Security=True;Connection Timeout=60; multipleactiveresultsets=true'
Phil Sandler
  • 27,544
  • 21
  • 86
  • 147
  • Format of the initialization string does not conform to specification starting at index 0. – Jason Oct 18 '10 at 20:04
  • however, if I use "provider connection string='Data Source=localhost;Initial Catalog=Login;Integrated Security=True;Connection Timeout=60; multipleactiveresultsets=true'" then it says it requires metadata... – Jason Oct 18 '10 at 20:06
  • "The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid." – Jason Oct 18 '10 at 20:15