无法创建类型为“ApplicationDbContext"的对象.对于设计时支持的不同模式 [英] Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time

查看:25
本文介绍了无法创建类型为“ApplicationDbContext"的对象.对于设计时支持的不同模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.net core中添加数据库迁移时遇到以下错误

I face the following error when adding the migration of database in .net core

这是错误:

这是Startup中的代码:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options => 
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
           
    services.AddDefaultIdentity<ApplicationUser>().AddEntityFrameworkStores<ApplicationDbContext>();
    services.AddControllers();
}

这是 ApplicationDbContext 类:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    { }

    public DbSet<ApplicationUser> applicationUsers { get; set; }
}

这是ApplicationUser:

public class ApplicationUser : IdentityUser
{
    [Required]
    [Column(TypeName = "nvarchar(150)")]
    public string UserFName { get; set; }
    [Required]
    public string UserLName { get; set; }
}

推荐答案

看来你的继承是错误的.

Seems you are your inheritance is wrong.

public ApplicationDbContext : IdentityDbContext

应该是

public ApplicationDbContext : IdentityDbContext<ApplicationUser>

public ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole>

如果您还扩展角色类.

当您想使用扩展用户类(而不是 IdentityUser)创建上下文时

when you want to create an context with an extended user class (instead of IdentityUser)

这篇关于无法创建类型为“ApplicationDbContext"的对象.对于设计时支持的不同模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆