Asp.Net Core 2.1添加迁移不会从继承自IdentityUser的类中获取属性

问题描述 投票:0回答:2

我正在使用具有单独用户帐户的Web应用程序模板在VS2017 v15.8.2中处理ASP.Net Core 2.1.1 Web应用程序。

我想为IdentityUser添加更多属性,因此我创建了一个继承自ApplicationUserIdentityUser类。我将适当的更新应用于startup.cs和_loginPartial.cshtml。所有东西都编译,但是当我运行Add-Migration时,它不会在我的ApplicationUser类中获取属性。我有一个迁移类,但up和down方法没有属性。

我只有一个项目,因此在Package Manager Console中选择Default Project不是问题。

这是我的ApplicationUser类

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public int CompanyId { get; set; }
}

这是我的DBContext

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

这是Startup.cs中的服务,ConfigureService

        services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.
        GetConnectionString("AccountDbConnectionString")));

        services.AddDefaultIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

以下是程序包管理器控制台中的添加迁移

Add-Migration -c ApplicationDbContext V1.00.01 

这是由此产生的迁移

public partial class V10001 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {

    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {

    }
}

ApplicationUser类位于项目根目录下的Models文件夹中。

当我第一次创建项目时,IdentityUser被解析为初始的Asp.Net核心Web应用程序模板迁移,当我运行update-database命令时,它在数据库中创建了所有的AspNetUserxxx和AspNetRolexxx表。

但我不明白为什么EF Migrations没有看到ApplicationUser类,所以它可以将新属性添加到迁移类。

当我在网上查找示例时,看起来我正在做正确的事情来扩展IdentyUser属性。

我错过了什么?

编辑1:我在这个SO帖子中尝试了解决方案,但没有任何区别。 Entity Framework Core 2.0 add-migration not generating anything

asp.net-identity entity-framework-core asp.net-core-2.0 asp.net-core-2.1 ef-core-2.1
2个回答
1
投票

您需要在DBContext中添加ApplicationUser类。

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

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

然后添加迁移,然后更新数据库。


1
投票

改变你的ApplicationDbContext如下:

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

}

这里的截图有效:

enter image description here

[更新]:我发现@vivek nuna的答案也有效。你可以随意选择。

[更新]:如果遇到如下异常:

An unhandled exception occurred while processing the request.

InvalidOperationException:没有注册类型为“Microsoft.AspNetCore.Identity.UserManager`1 [Microsoft.AspNetCore.Identity.IdentityUser]”的服务。 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider,Type serviceType)

不要忘记更新_LoginPartial.cshtmlSignInManager引用UserManagerIdentityUser,即更改代码:

@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
© www.soinside.com 2019 - 2024. All rights reserved.