EntityFramework 6 Code-based configuration of conn

2019-02-19 18:12发布

问题:

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:

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)



回答2:

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