错误CS1061'IdentityBuilder'不包含'AddEntityFrameworkStores'

时间:2017-10-10 16:28:39

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

我正在尝试将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?)

enter image description here

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

4 个答案:

答案 0 :(得分:6)

有关如何执行此操作的信息,请参阅此链接中的评论https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x

Change ApplicationUser to inherit from IdentityUser<int>

Create a new class ApplicationRole that inherits from IdentityRole<int>

Change ApplicationDbContext to inherit from IdentityDbContext<ApplicationUser, ApplicationRole, int>

Change startup.cs for services.AddIdentity to use ApplicationRole

Change UrlHelperExtensions methods to take a generic <T> with T userId in the signature

Change ManageController's LinkLoginCallback's call to await _signInManager.GetExternalLoginInfoAsync(user.Id.ToString())

Add the following line to the ApplicationDbContext OnModelCreating method (after the base.OnModelCreating call)

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

答案 1 :(得分:4)

对我来说,解决此问题的方法只是通过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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

我通过更改ApplicationDbContextIdentityDbContext<ApplicationUser>继承来解决了这个问题