Search code examples
c#entity-frameworkarchitectureapp-confign-tier-architecture

EF Connection string without App.Config in startup project


I'm trying to abstract EF in the DAL project of my solution. I've already reached the point that EF is only known in this project.

I've created a new Console.Test application which uses my BLL, which uses my DAL. Now this new console application requires an App.Config file which contains the EF connection string. My next step would be to get rid of this App.Config file aswell, since i would like to use my BLL on several platforms.

So i've created a partial class of my derived DbContext to add an extra constructor which accepts a full connection string. In my DAL i've also created a new static method for getting the connectionstring:

public static string GetSqlConnectionString(string serverName, string databaseName)
    {
        SqlConnectionStringBuilder providerCs = new SqlConnectionStringBuilder();

        providerCs.DataSource = serverName;
        providerCs.InitialCatalog = databaseName;
        providerCs.IntegratedSecurity = true;
        providerCs.UserID = "*****";
        providerCs.Password = "******";

        var csBuilder = new EntityConnectionStringBuilder();

        csBuilder.Provider = "System.Data.SqlClient";
        csBuilder.ProviderConnectionString = providerCs.ToString();

        csBuilder.Metadata = "res://Prodomus.DAL/DataObjects.Entities.Model.csdl|res://Prodomus.DAL/DataObjects.Entities.Model.ssdl|res://Prodomus.DAL/DataObjects.Entities.Model.msl";

        return csBuilder.ToString();
    }

Now my problem is that i keep getting this error message stating: 'The underlying provider failed to open.'. I know for sure that the credentials i've used are correct, this also goes for the servername aswell as the databasename. Am i missing something here? Any help will be very much appreciated.

Thnx in advance!


Solution

  • You cannot mix Integrated security and username/password