简体   繁体   English

DbContext不可构造

[英]DbContext is not constructible

I have just started playing around with MVC5, having already had a fair amount of exposure to MVC4. 我刚刚开始玩MVC5,已经对MVC4有了很多了解。

Firstly, I decided to call ApplicationUser in my application UserProfile (so UserProfile inherits from IdentityUser). 首先,我决定在我的应用程序UserProfile调用ApplicationUser (因此UserProfile继承自IdentityUser)。 I also want to have a single DbContext so relationships between the UserProfile and other Entities are easily used. 我还希望有一个DbContext,以便轻松使用UserProfile和其他实体之间的关系。 This error occurs with or without a connection string (no connection string creates a localdb mdf file). 有或没有连接字符串都会发生此错误(没有连接字符串会创建localdb mdf文件)。

The database was building, but it kept naming the database DefaultConnection even though the connection string etc had not been named this. 数据库正在构建中,但是即使未将连接字符串等命名为DefaultConnection,它仍在命名数据库DefaultConnection。 I realised I was not passing the connection string into my DbContext ctor. 我意识到我没有将连接字符串传递到我的DbContext ctor中。 I then changed this to pass connection string namer in the ctor of my DbContext file. 然后,我将其更改为在DbContext文件的ctor中传递连接字符串命名器。 Since doing so I can get this error. 由于这样做,我会收到此错误。

The target context 'Mesanderson.Infrastructure.DatabaseConfig.MesandersonDb' is not constructible. 目标上下文“ Mesanderson.Infrastructure.DatabaseConfig.MesandersonDb”是不可构造的。 Add a default constructor or provide an implementation of IDbContextFactory. 添加默认构造函数或提供IDbContextFactory的实现。

With Migrations installed on my data project I have this structure 在我的数据项目上安装了Migrations之后,我就拥有了这种结构

Configure Database File, InitializeDatabase called from Global.asax 配置数据库文件,从Global.asax调用的InitializeDatabase

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 最后,DbContext文件

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. (如果相关的话)我还替换了帐户控制器上的OOTB代码以使用UserManager工厂,因为我不想在整个应用程序中使用连接字符串名称。

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. 我看过的任何地方都说错误是因为DbContext中没有无参数的ctor,但是我有。

A suggestion accepted by the OP as an answer: OP接受的建议作为答案:

Try to make both your class and the constructor public instead of internal . 尝试将您的类和构造函数public而不是internal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM