0

I just updated from EF5 to EF6. This got me introduced to the DbConfiguration, since I needed one of these so that I wouldn't need to add EntityFramework as dependency to my other projects (solution has 8 projects). Everything was working until I decided to make my DbConfiguration class (inherited) to be generic, so that other DbContext classes could use it. After doing so, I started getting the error "Failed to create instance of type 'Repository.Contexts.ContextConfiguration`1[Repository.Contexts.GlobalContext]'. The type must not be generic."

Here's the [new] DbConfiguration:

using System.Data.Entity;
using System.Data.Entity.SqlServer;

namespace Repository.Contexts
{
    // Old impl did not have a constraint. Instead, the type was directly used in the CreateDatabaseIfNotExists's param
    public class ContextConfiguration<TContext> : DbConfiguration where TContext : DbContext
    {
        public ContextConfiguration()
        {
            SetDatabaseInitializer(new CreateDatabaseIfNotExists<TContext>());
            SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
        }
    }
}

The configuration is used as such:

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

[DbConfigurationType(typeof(ContextConfiguration<GlobalContext>))]
public class GlobalContext : DbContext
{
    // DbSets...
    // Constructor...
    // OnModelCreating...
}

Does every context have to have its own configuration in order to achieve what I'm trying to do?

2
  • Why you need the DbConfiguration? You have a seed method ? Commented Jul 6, 2016 at 18:41
  • @BassamAlugili without it, I was getting the "No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file." exception. Some posts said that, to fix this, I needed to include EntityFramework as reference to all the projects that uses my DAO project (as stated above). Commented Jul 6, 2016 at 18:49

0

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.