How to add entity framework 6 provider in code?

c# connection-string entity-framework-6

Question

In a C# application I'm working on, Entity Framework 6 works flawlessly. The app.config is generated together with all the required configuration when the Model is created. I use the connection string builder these days because I don't like having things in the app.config. I was able to delete everything from the app.config file except for the following:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

If I take this out, nothing happens. How then do I convert that configuration into C# code? How can I go about doing that? Although I looked at the code-based setup (http://msdn.microsoft.com/en-us/data/jj680699), it was ineffective.

This is how my partial class, which generates the connection string, looks:

public partial class LogEntities
    {
        public LogEntities(string serverName)
            : base(GetConnectionString(serverName))
        {
        }

        public static string GetConnectionString(string serverName)
        {
            // Specify the provider name, server and database.
            const string databaseName = "_LOG";

            // Initialize the connection string builder for the underlying provider.
            SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder
            {
                DataSource = serverName,
                InitialCatalog = databaseName,
                IntegratedSecurity = true,
                MultipleActiveResultSets = true
            };

            // Initialize the EntityConnectionStringBuilder.
            System.Data.EntityClient.EntityConnectionStringBuilder entityBuilder = new System.Data.EntityClient.EntityConnectionStringBuilder();
            entityBuilder.Provider = "System.Data.SqlClient";

            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = sqlBuilder.ToString();

            // Set the Metadata location.
            entityBuilder.Metadata = @"res://*/myproject.LogModel.csdl|res://*/myproject.LogModel.ssdl|res://*/myproject.LogModel.msl";

            return entityBuilder.ConnectionString;
        }
    }

Any assistance is greatly appreciated.

1
9
11/6/2013 3:33:24 PM

Popular Answer

You can utilize Code Base configuration in EF6. See the this article for further information. It demonstrates how to change the factory default connection (use theSetDefaultConnectionFactory method). You use the to configure the providerSetProviderServices method.

13
7/3/2017 9:48:10 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