【问题标题】:Error CS1061 'IdentityBuilder' does not contain a definition for 'AddEntityFrameworkStores'错误 CS1061“IdentityBuilder”不包含“AddEntityFrameworkStores”的定义
【发布时间】:2023-03-20 10:45:02
【问题描述】:

我正在尝试将 ASPNET Core 2.0 中的主键更改为 Guidlong

我在这两个链接中都尝试了以下示例 Link1Link2

但是,在 Startup 类中出现以下错误

 Error  CS1061  'IdentityBuilder' does not contain a definition for
 'AddEntityFrameworkStores' and no extension method 'AddEntityFrameworkStores' accepting 
 a first argument of type 'IdentityBuilder' could be found (are you missing a using 
 directive or an assembly reference?)

我认为原因是这些示例基于较旧的 ASPNET Core 1.0。如何在 ASPNET Core 2.0 中更改 Identity 的主键?

【问题讨论】:

标签: c# asp.net-core-2.0


【解决方案1】:

如何做到这一点的功劳可以在这个链接https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x的cmets中找到

将 ApplicationUser 更改为从 IdentityUser 继承

创建一个继承自 IdentityRole 的新类 ApplicationRole

将 ApplicationDbContext 更改为继承自 IdentityDbContext<ApplicationUser, ApplicationRole, int>

将 services.AddIdentity 的 startup.cs 更改为使用 ApplicationRole

更改 UrlHelperExtensions 方法以在签名中使用带有 T userId 的通用

将 ManageController 的 LinkLoginCallback 调用改为

await _signInManager.GetExternalLoginInfoAsync(user.Id.ToString())

将以下行添加到 ApplicationDbContext OnModelCreating 方法(在 base.OnModelCreating 调用之后)

builder.Entity<ApplicationUser>().Property(p => p.Id).UseSqlServerIdentityColumn();

如果使用Guid,则替换

builder.Entity<ApplicationUser>().Property(p => p.Id).UseSqlServerIdentityColumn(); 

builder.Entity<ApplicationUser>().Property(p => p.Id).ValueGeneratedOnAdd();

所有更改如下

public class ApplicationUser : IdentityUser<Guid>
{
}

public class ApplicationRole : IdentityRole<Guid>
{
    
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
    Public ApplicationDbContext(DbContextOptions < ApplicationDbContext > Options): Base (Options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        // For Guid Primary Key
        builder.Entity<ApplicationUser>().Property(p => p.Id).ValueGeneratedOnAdd();

        // For int Primary Key
        //builder.Entity<ApplicationUser>().Property(p => p.Id).UseSqlServerIdentityColumn();
    }
}

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

    services.AddIdentity<ApplicationUser, ApplicationRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    // Add application services.
    services.AddTransient<IEmailSender, EmailSender>();

    services.AddMvc();
}

public static class UrlHelperExtensions
{
    public static string EmailConfirmationLink<T>(this IUrlHelper urlHelper, T userId, string code, string scheme)
    {
        return urlHelper.Action(
            action: nameof(AccountController.ConfirmEmail),
            controller: "Account",
            values: new { userId, code },
            protocol: scheme);
    }

    public static string ResetPasswordCallbackLink<T>(this IUrlHelper urlHelper, T userId, string code, string scheme)
    {
        return urlHelper.Action(
            action: nameof(AccountController.ResetPassword),
            controller: "Account",
            values: new { userId, code },
            protocol: scheme);
    }
}

....
var info = await _signInManager.GetExternalLoginInfoAsync(user.Id.ToString());

【讨论】:

【解决方案2】:

为我解决的只是通过 NuGet 添加“Microsoft.AspNetCore.Identity.EntityFrameworkCore

如果我在将 Identity 添加到我的 Core 3.1 项目时遇到了类似的问题,那么这可能已被移到单独的 NuGet 包中。 (I updated my .NET CORE project from 2.1 to 3.1, yet I have some errors in the class Startup)

【讨论】:

    【解决方案3】:

    我通过将ApplicationDbContext 更改为从IdentityDbContext&lt;ApplicationUser&gt; 继承解决了这个问题

    【讨论】:

      【解决方案4】:

      从 Nuget 包管理器安装 Microsoft.AspNetCore.Identity.EntityFrameworkCore 后,我遇到了同样的问题 我的错误已解决。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-21
        • 2018-04-11
        • 1970-01-01
        • 2020-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-31
        相关资源
        最近更新 更多