DbContext is not constructible

asp.net-mvc-5 c# dbcontext entity-framework-6

Question

Having spent some time with MVC4 already, I've only recently begun experimenting with MVC5.

First, I chose to dialApplicationUser in my submissionUserProfile (Therefore, UserProfile descended from IdentityUser.) Additionally, I want a single DbContext to make it simpler to use relationships between the UserProfile and other Entities. With or without a connection string, this issue happens (no connection string creates a localdb mdf file).

Even though the connection string and other components had not been given this name, the database maintained referring to itself as DefaultConnection as it was being built. I realized I wasn't giving my DbContext ctor the connection string. I later modified this to pass the connection string namer in my DbContext file's ctor. I can now encounter this issue as a result.

The target context 'Mesanderson.Infrastructure.DatabaseConfig.MesandersonDb' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.

I have this structure with Migrations installed on my data project.

set up the database file,InitializeDatabase originating from Global.asax

public class ConfigureDatabase
{
    public void InitializeDatabase()
    {
        Database.SetInitializer<Infrastructure.DatabaseConfig.MesandersonDb>(new Infrastructure.DatabaseConfig.DatabaseInitializer());
    }
}

Initializer document

internal class DatabaseInitializer : MigrateDatabaseToLatestVersion<Infrastructure.DatabaseConfig.MesandersonDb, Migrations.Configuration>
{

}

Config File for Migrations (auto migrations on during dev)

internal sealed class Configuration : DbMigrationsConfiguration<Mesanderson.Infrastructure.DatabaseConfig.MesandersonDb>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
        MigrationsDirectory = @"DatabaseConfig/Migrations";
    }

    protected override void Seed(Mesanderson.Infrastructure.DatabaseConfig.MesandersonDb context)
    {

    }
}

The DbContext file comes last.

internal class MesandersonDb : IdentityDbContext<Entities.Models.UserProfile>
{
    internal MesandersonDb() : base("MesandersonDb") { }

    DbSet<Entities.Models.Expense> Expenses { get; set; }
    DbSet<Entities.Models.BlogPost> BlogPosts { get; set; }
    DbSet<Entities.Models.Client> Clients { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new DatabaseConfig.ModelConfig.BlogCommentConfiguration());
        modelBuilder.Configurations.Add(new DatabaseConfig.ModelConfig.BlogPostConfiguration());
        modelBuilder.Configurations.Add(new DatabaseConfig.ModelConfig.ClientConfiguration());
        modelBuilder.Configurations.Add(new DatabaseConfig.ModelConfig.ExpenseConfiguration());
        modelBuilder.Configurations.Add(new DatabaseConfig.ModelConfig.UserProfileConfiguration());
    }
}

(If applicable) Since I didn't want connection string names to be used across the application, I also changed the OOTB code on the account controller to use a UserManager factory.

public static class UserManagerFactory
{
    public static UserManager<UserProfile> GetUserManager()
    {
        return new UserManager<UserProfile>(new UserStore<UserProfile>(new Mesanderson.Infrastructure.DatabaseConfig.MesandersonDb()));
    }
}

Everywhere I've looked, it says the paramless ctor in the DbContext is missing, but I do have one.

1
1
2/2/2014 9:43:07 PM

Accepted Answer

The OP has accepted the following advice as an answer:

Make an effort to make your class and the constructor.public in place ofinternal .

2
2/2/2014 10:16:32 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