Jens
Jens

Reputation: 477

Entity framework 6 without config file?

I have an application that can not use a config file and has to set up the connectionstring for the entity context by code (with EntityConnectionStringBuilder).

The problem comes however with this:

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'.

Since I can not use the standard app.config file, am I screwed in EF6? It was not a problem in EF4 if I recall correctly. So the settings inside the entityFramework-tag is something I wish to set in code.

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>

Upvotes: 11

Views: 11061

Answers (2)

Pawel
Pawel

Reputation: 31610

In EF6 you can use Code-based Configuration for Entity Framework specific setup. (Also see How to add entity framework 6 provider in code?). Then you can build connection string and pass it to the ctor of the context.

Upvotes: 4

Peter Lea
Peter Lea

Reputation: 1751

I'm using EF 6 without an App.config file at all I just pass a connection string to my DBContext (well the connection is read out of a config file elsewhere but that's up to you). Just Remove all the EF stuff from the config file.

Here's how I got it to work.

public partial class MyDbContext : DbContext, IMyDbContext
{
    public MyDbContext(string connectionString,
                                         DbValidationMode validationMode = DbValidationMode.Enabled,
                                         DbLazyLoadingMode lazyLoadingMode = DbLazyLoadingMode.Disabled,
                                         DbAutoDetectMode autoDetectMode = DbAutoDetectMode.Enabled)
            : base(((EntityConnectionStringBuilder)new EntityConnectionStringBuilder()
            {
                Provider = "System.Data.SqlClient",
                ProviderConnectionString = connectionString,
                Metadata = @"res://*/Contexts.Test.MyModel.csdl|res://*/Contexts.Test.MyModel.ssdl|res://*/Contexts.Test.MyModel.msl"
            }).ToString())
            { 
                    this.Configuration.ValidateOnSaveEnabled = validationMode.Equals(DbValidationMode.Enabled);
                    this.Configuration.LazyLoadingEnabled = lazyLoadingMode.Equals(DbLazyLoadingMode.Enabled);
                    this.Configuration.AutoDetectChangesEnabled = autoDetectMode.Equals(DbAutoDetectMode.Enabled);
            }
... Etc  

Then I just inject this context into all my services.

I'm using Database First design so I found it easier to modify the T4 Templates.

There may be nicer ways of doing this but I've not ran into any problems at all.

Upvotes: 14

Related Questions