EntityFramework 6 Code-based configuration of connection string for database-first

.net c# entity-framework entity-framework-6

Question

I'm attempting to make an existing application work without an app.config (it is required due to a very specific environment). Problem is that it's heavily relying on EntityFramework 6 to work with an SQL-Server.

I'm trying to use a code-based configuration, but I can't figure out how to provide a correct connection string through my configuration class.

I made a configuration class:

public class MyConfiguration : DbConfiguration
{
    public MyConfiguration()
    {
        SetDefaultConnectionFactory(new MyConnectionFactory());
        SetProviderServices("System.Data.SqlClient", System.Data.Entity.SqlServer.SqlProviderServices.Instance);
    }
}

Then provided it to my DbContext (Generated by EF automatically from bd):

[DbConfigurationType(typeof(MyConfiguration))]
public partial class TestModelEntities
{           
}

With a custom connection factory:

public class MyConnectionFactory : IDbConnectionFactory
{
    public DbConnection CreateConnection(string nameOrConnectionString)
    {
        var newConnStringBuilder = new SqlConnectionStringBuilder
        {
            UserID = "user",
            Password = "pass",
            InitialCatalog = "databaseName",
            DataSource = "serverName"
        };

        var entityConnectionBuilder = new EntityConnectionStringBuilder
        {
            Provider = "System.Data.SqlClient",
            ProviderConnectionString = newConnStringBuilder.ToString(),
            Metadata = @"res://*/TestModel.csdl|
                            res://*/TestModel.ssdl|
                            res://*/TestModel.msl"
        };

        var newDbConnect = new EntityConnection(entityConnectionBuilder.ToString());
        return newDbConnect;
    }
}

However. When I test it, I get an UnintentionalCodeFirstException. Why? What am I missing?

1
2
11/20/2018 5:01:16 PM

Accepted Answer

You should provide connection string to your context via :base(connectionString). Create a class as below:

public class ConnectionStringBuilder
{
    public static string Construct()
    {
        var newConnStringBuilder = new SqlConnectionStringBuilder
        {
            UserID = "user",
            Password = "pass",
            InitialCatalog = "databaseName",
            DataSource = "serverName"
        };

        var entityConnectionBuilder = new EntityConnectionStringBuilder
        {
            Provider = "System.Data.SqlClient",
            ProviderConnectionString = newConnStringBuilder.ToString(),
            Metadata = @"res://*/TestModel.csdl|
                            res://*/TestModel.ssdl|
                            res://*/TestModel.msl"
        };

        return entityConnectionBuilder.ToString();
    }
}

Then modify your Context constructor to look like this:

public DbContext()
    : base(ConnectionStringBuilder.Construct())
{
}

It should work fine now. (source)

5
7/28/2016 12:47:24 PM

Popular Answer

Exposing a static method could pose dangers in concurrent user scenario. Try to use singleton pattern to create a DB connection string.



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