Domanda

I have a solution with 3 projects. A console project and two class library projects.

Each class project has an EDM item added with one Entity.

So I use Model First approach.

Each class project has its own app.config with:

<connectionStrings>
<add name="FilingDBContainer" connectionString="Data Source=TESTPC\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;Pooling=False" />
</connectionStrings>

<connectionStrings>
<add name="ResponderDBContainer" connectionString="Data Source=TESTPC\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;Pooling=False" />
</connectionStrings>

In the console project which I can run I have this code:

var fileContext = new FilingDBContainer();
var files = fileContext.FileSet;
for (int i = 0; i < 10; i++)
{
    files.Add(new File { Id = i + 1, Filename = "Test" + i });
}

var responderContext = new ResponderDBContainer();
var responders = responderContext.ResponderSet;
for (int i = 0; i < 10; i++)
{
    responders.Add(new Responder { Id = i + 1, Respondername = "Test" + i });
}

When the first file is added I get an exception:

No connection string named 'FilingDBContainer' could be found in the application config file.

For me it seems the connectionString should be put in the console project but I do not want to the put con string there. Each class project should have its own con string!

How can I do that?

È stato utile?

Soluzione

Your console app should have one config file with this entry:

<connectionStrings>
<add name="FilingDBContainer" connectionString="Data Source=TESTPC\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;Pooling=False" />
<add name="ResponderDBContainer" connectionString="Data Source=TESTPC\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;Pooling=False" />
</connectionStrings>

Connection strings are loaded from the config of the loading application. If you want to automatically load config files for assemblies, you'll have to write code to manually query it.

Altri suggerimenti

The end executable will have its own config file. The class libraries config is not used. Add a .config for your console application that controls the environment of the libraries it consumes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top