2017-10-10 9 views
3

ASPNETコア2.0の主キーをGuidまたはlongに変更しようとしています。エラーCS1061 t 'IdentityBuilder'に 'AddEntityFrameworkStores'の定義が含まれていません

私はその理由を考えるスタートアップクラスで、私は次のエラー

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

を取得し、しかし、両方のこれらのリンク Link1Link2

で次の例を試してみました例は古いASPNET Core 1.0に基づいています。 ASPNET Core 2.0のIDの主キーを変更するにはどうすればよいですか? Guidを使用している場合、これを行う方法について

+0

下回っているbuilder.Entity<ApplicationUser>().Property(p => p.Id).ValueGeneratedOnAdd();

builder.Entity<ApplicationUser>().Property(p => p.Id).UseSqlServerIdentityColumn();を置き換えます同じ問題がありますあなたはこれに対する解決策を見つけましたか? – devC

答えて

2

クレジットは、このリンクにコメント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(); 

で見つけることができますし、私はすべての変更が

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()); 
関連する問題