문제

I have just started playing around with MVC5, having already had a fair amount of exposure to MVC4.

Firstly, I decided to call ApplicationUser in my application UserProfile (so UserProfile inherits from IdentityUser). I also want to have a single DbContext so relationships between the UserProfile and other Entities are easily used. This error occurs with or without a connection string (no connection string creates a localdb mdf file).

The database was building, but it kept naming the database DefaultConnection even though the connection string etc had not been named this. I realised I was not passing the connection string into my DbContext ctor. I then changed this to pass connection string namer in the ctor of my DbContext file. Since doing so I can get this error.

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

With Migrations installed on my data project I have this structure

Configure Database File, InitializeDatabase called from Global.asax

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

Initializer file

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

}

Migrations Config File (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)
    {

    }
}

Finally, the DbContext file

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());
    }
}

(incase relevant) I also replaced the OOTB code on the account controller to use a UserManager factory as I dont want connection string names used all over the application.

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

Anywhere I have looked says the error is because there is no paramless ctor in the DbContext, but I have that.

도움이 되었습니까?

해결책

A suggestion accepted by the OP as an answer:

Try to make both your class and the constructor public instead of internal.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top