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 challenge is to build a generic DAL using Entity Framework 6, Repository Pattern and N-Tier Solution Architecture.

Synopsis: Using this approach https://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework, I created my DAL and BLL. All good and well, Unittests completes without a hitch. Nice.

Challenge: I then created a Presentation Layer, which includes an Excel AddIn. But since the database used can be different from each client, the database ConnectionString needs to be changed from the client. OK, adding a app-config to the Excel-template project… doesn't work. No matter what I do, I get no data back from the database.

Now I need to remove the need for an app.config and instead just supply the ConnectionString from the Client, when initialising the BL.

I tried the suggestions from Entity Framework - Database First without config and How can l use Entity Framework without App.config, but I cannot seem the get it to fit nicely into my challenge. I did use some of the solutions mentioned in those, but since my DAL is using the Repository Pattern, I cannot quite get my limited head around how to implement the solutions mentioned...

What I have done: I added a DbConfiguration-class in the DataAccessLayer, which implements multiple constructors. (I cannot figure out HOW to hit the other constructors - only the parameterles constructor, but that's an issue for another question). My constructor for ythis classe looks like this:

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 just cant seem to figure out HOW to use my EntityConnection object in the initialisation of my DbConfiguration. Neither of the 2 last lines of code seems to be working properly.

In my DbContext, i changed the Constructor 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;
}

This though, only brings out the dreaded UnIntentionalCodeFirstException, and I am now stuck.

Question:

How do I get the ConnectionString from my PL all the way through to the DAL? I would try to create a non-parameterless Repository-constructor, which would accept a SQL-sonnectionstring as parameter, but how?

I know this is a huge question, but I have tried to give as much information I could without causing a SystemOutOfMemoryException and tried to boil my issue down to a single question - more questions WILL arise, later on, but for now this must suffice…

Hope some of you prodigies can give me a pointer or two…

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

Accepted Answer

OK, I never got it working like I wanted to, so i changed my DomainModel to NOT use an EDMX file - which is the entire cause of the final error i got…

SÃ¥ basically I've changed the EF modelling from Database First to Code-First....

I also added EntityFramework package to my Excel-Addin project.

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