2

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 Answer 1

5

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Heh. Surprisingly simple solution. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.