错误CS1061"IdentityBuilder"不包含"AddEntityFrameworkStores"的定义 [英] Error CS1061 'IdentityBuilder' does not contain a definition for 'AddEntityFrameworkStores'
问题描述
我正在尝试将ASPNET Core 2.0中的主键更改为Guid
或long
.
I'm trying to change the primary key in ASPNET Core 2.0 to Guid
or long
.
我在这两个链接中都尝试了以下示例 Link1 和 Link2
I've tried the following examples in both these links Link1 and Link2
但是,在Startup类中,出现以下错误
However, in the Startup class I get the following error
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中更改身份的主键?
I think the reason is the examples are based on the older ASPNET Core 1.0. How can I change the primary key for Identity in ASPNET Core 2.0?
推荐答案
在此链接中的注释中可以找到有关如何执行此操作的信誉 https://docs.microsoft.com/zh-cn/aspnet/core/migration/1x-to-2x/identity -2x
Credit on how to do this can be found in the comments in this link 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();
If using Guid
then replace builder.Entity<ApplicationUser>().Property(p => p.Id).UseSqlServerIdentityColumn();
with 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());
这篇关于错误CS1061"IdentityBuilder"不包含"AddEntityFrameworkStores"的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!