No connection string found in App config using external Start-up application

.net c# dll entity-framework-6

Question

I am creating a dll project in C# using Visual Studio 2013 .Net 4.5. The project contains an entity model using EF6. My dll will be called as a plugin to an external application, that belongs to a software suite for some hardware products.

I have visual studio set up so that when I press debug it launches this external application (i will refer to as the gateway) which in turn makes calls to my dll, allowing me to debug.

The issue occurs when my dll tries to make calls to the entity model. I get:

InvalidOperationException -- An unhandled exception of type 'System.InvalidOperationException' occured in ENTITYFRAMEWORK.dll

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

Now, my config file has, very clearly, the required connection string which was added by VS when I set up the model. I have read all of the similar threads to this issue, and adding the connection string to the config is not the problem.

The gateway does have knowledge of the real DB instance and is given the connection string for that in a configuration. However, the entity connection string and the instance connection string differ, because of the entity jargon that gets added on by VS and EF.

I am able to create a console app which makes calls to my dll, capable of making the enity calls. I had to give the console app a reference to my dll and entity framework as well as add the connection string to the console apps config file.

This has led me to believe that, any app that makes calls into my dll must have knowledge of EF and the connection string.

Is this the case? If so, it would appear to me that I will be unable to use EF for this project because I will have no way to make the gateway aware of EF.

Thanks for your input!

1
0
2/10/2015 4:05:01 PM

Accepted Answer

So what I would do in this case is make a new Context which inherits from your current context and in the constructor pass in the connection string.

Alternatively you can also create a connection string that you can then pass into the constructors overload for your context.

public string GetConnectionString()
    {
        string connectionString = new EntityConnectionStringBuilder
        {
            Metadata = "res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl",
            Provider = "System.Data.SqlClient",
            ProviderConnectionString = new SqlConnectionStringBuilder
            {
                InitialCatalog = ConfigurationManager.AppSettings["SystemDBName"],
                DataSource = ConfigurationManager.AppSettings["SystemDBServerName"],
                IntegratedSecurity = false,
                UserID = ConfigurationManager.AppSettings["SystemDBUsername"],
                Password = ConfigurationManager.AppSettings["SystemDBPassword"],
                MultipleActiveResultSets = true,
            }.ConnectionString
        }.ConnectionString;

        return connectionString;
    }
0
2/10/2015 4:10:33 PM

Popular Answer

Can't you compose the Entity connectionstring based on the sql connection string?

Otherwhise have a look at this question: How to have DLL specific config file?

Basicly you are supplying a config for the dll file. So besides of myapplication.exe.config you can have a myddl.dll.config



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