Entity Framework without App.config, Database first, Repository Pattern, N-tier Solution Architecture

c# entity-framework-6 n-tier-architecture repository-pattern

Question

OK, now I give up. My goal is to use Entity Framework 6, the Repository Pattern, and N-Tier Solution Architecture to create a generic DAL.

Synopsis: I made my DAL and BLL using this method https://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework. Unittests run successfully without any issues. Nice.

Challenge: Next, I made a Presentation Layer with a Office AddIn in it. However, each client may use a different database, thus the client must modify the database ConnectionString. It doesn't work to add an app configuration to the Excel template project, I see. No matter what I do, the database never returns any data to me.

I now need to initialise the BL without the requirement for an app.config and just give the ConnectionString from the Client.

I tried using the advice from Database-first Entity Framework without configuration and What would Entity Framework look like without App.config?, but I was unable to make it work well for my issue. I have used a few of the solutions suggested there, but since my DAL follows the Repository Pattern, I find it difficult to put the answers into practise.

What I did was include a DbConfiguration-class that implements numerous constructors in the DataAccessLayer. (I am unable to reach the other constructors; just the parameterles function Object() { [native code] }; however, that is a problem for a different inquiry.) This is how my function Object() { [native code] } for this class looks:

private DefaultConfiguration()
{

    SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder()
    {
        DataSource = database,
        InitialCatalog = initialCatalog,
        UserID = userId,
        Password = password,
        IntegratedSecurity = useTrustedConnection
    };

    // Initialize the EntityConnectionStringBuilder.
    EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder()
    {
        Provider = providerName,
        // Set the provider-specific connection string.
        ProviderConnectionString = sqlBuilder.ToString(),
        // Set the Metadata location.
        Metadata = @"res://*/CommonModel.csdl|res://*/CommonModel.ssdl|res://*/CommonModel.msl"
    };

    SetExecutionStrategy("System.Data.SqlClient", () => new DefaultExecutionStrategy(), database);      
}

EntityConnection entityCon = new EntityConnection(entityBuilder.ToString());
//SetDefaultConnectionFactory(new SqlConnectionFactory(entityCon.ConnectionString));
//SetDefaultConnectionFactory(new SqlConnectionFactory(sqlBuilder.ToString()));

I'm simply having trouble understanding HOW to utilise my EntityConnection object while setting up my DbConfiguration. The last two lines of code don't seem to be operating correctly.

I updated the Constructor in my DbContext from

public CommonEntities()
    : base("name=CommonEntities")
{
    this.Configuration.LazyLoadingEnabled = false;
    this.Configuration.ProxyCreationEnabled = false;
}

to

public CommonEntities(string ConnectionString)
    : base(ConnectionString)
{
    this.Configuration.LazyLoadingEnabled = false;
    this.Configuration.ProxyCreationEnabled = false;
}

However, this merely triggers the dreaded UnIntentionalCodeFirstException, leaving me helpless.

Question:

How can I transfer the ConnectionString from my PL to the DAL? I'd want to attempt making a Repository function Object() { [native code] } that takes a SQL connection string as a parameter but how?

I realise this is a big question, but I've attempted to be as detailed as possible without throwing a SystemOutOfMemoryException and to simplify my problem into one question. More questions WILL come up later, but for now, this must do.

I'm hoping one or two of you prodigies can offer me some advice.

1
0
8/3/2018 12:05:46 PM

Accepted Answer

ZZZ_tmp
0
8/9/2018 1:30:55 PM


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