2017-08-23 18 views
9

Tôi đang cố gắng để kết thúc cập nhật lên NET Lõi 2.0 nhưng một vài lỗi bật lên:Không thể kế thừa từ IdentityUser và IdentityRole trong ASP.NET 2.0 Lõi

Vấn đề:

Tôi có hai lớp, ApplicationRole và ApplicationUser kế thừa một số thuộc tính từ IdentityRole và IdentityUser.

Với bản cập nhật lên Core 2.0, tôi nhận được các lỗi sau, tuyên bố rằng không thể tìm thấy IdentityRole và IdentityUser.

Tuy nhiên, gói Microsoft.AspNetCore.Identity.EntityFrameworkCore 2.0 được cài đặt trong các phiên bản mới của .NET Lõi

Về IdentityRole:

nhắn 1:

Loại hoặc namespace tên 'IdentityRole 'không thể tìm thấy (là nó thiếu một chỉ thị sử dụng hoặc một tham chiếu lắp ráp?)

nhắn 2:

Các 'Application.Models.ApplicationRole' loại không thể được sử dụng như một tham số của loại 'TRole' trong các loại generic hoặc phương pháp 'IdentityDbContext'. Không có chuyển đổi tham chiếu ngầm từ 'Application.Models.ApplicationRole' thành 'Microsoft.AspNetCore.Identity.IdentityRole'.

Định nghĩa của ApplicationRole:

public class ApplicationRole:IdentityRole 
{ 
    public string Description { get; set; } 
    public DateTime CreatedDated { get; set; } 
    public string IPAddress { get; set; } 
} 

Về IdentityUser:

nhắn 1:

Loại hoặc namespace tên 'IdentityUser' không thể tìm thấy (là nó thiếu một chỉ thị sử dụng hoặc tham khảo một hội)

nhắn 2:?

Các 'Application.Models.ApplicationUser' loại không thể được sử dụng như một tham số của loại 'TUser' trong loại chung hoặc phương pháp 'IdentityDbContext'. Không có chuyển đổi tham chiếu ngầm định từ 'Application.Models.ApplicationUser' thành 'Microsoft.AspNetCore.Identity.IdentityUser'.

Định nghĩa của ApplicationUser

public class ApplicationUser:IdentityUser 
{ 
    public string Name { get; set; } 
} 

Hướng dẫn sau để xác định các lớp học này và sử dụng của họ đang ở đây:

http://www.c-sharpcorner.com/article/asp-net-core-mvc-authentication-and-role-based-authorization-with-asp-net-core/

Với sự thay đổi đến Core 2.0 Tôi tự hỏi như thế nào IdentityRole và IdentityUser đã thay đổi để tôi không thể kế thừa từ chúng nữa.

Trả lời

14

Đã giải quyết.

Gói giữ các lớp đó Microsoft.AspNetCore.Identity.EntityFrameworkCore đã thay đổi. Để truy cập vào những clases (IdentityUserIdentityRole) người ta phải thêm

using Microsoft.AspNetCore.Identity; 

Với điều này, vấn đề biến mất.

+0

đã làm bạn tìm hiểu cách sử dụng Vai trò trên .Net Core 2.0? Nếu bạn có thể vui lòng đưa ra một ví dụ – YTerle

+0

@YTerle xem lại câu trả lời mới. – tchelidze

4

ICollection<IdentityUserRole<int>> Roles, ICollection<IdentityUserClaim<int>> ClaimsICollection<IdentityUserLogin<int>> Logins thuộc tính điều hướng đã bị xóa khỏi Microsoft.AspNetCore.Identity.IdentityUser.

Bạn nên xác định chúng bằng tay

public class MyUser : IdentityUser 
{     
    public virtual ICollection<IdentityUserRole<int>> Roles { get; } = new List<IdentityUserRole<int>>(); 

    public virtual ICollection<IdentityUserClaim<int>> Claims { get; } = new List<IdentityUserClaim<int>>(); 

    public virtual ICollection<IdentityUserLogin<int>> Logins { get; } = new List<IdentityUserLogin<int>>(); 
} 

Để ngăn chặn các phím nước ngoài trùng lặp khi chạy EF Lõi Migrations, thêm dòng sau vào lớp IdentityDbContext của bạn OnModelCreating phương pháp

protected override void OnModelCreating(ModelBuilder builder) 
{ 
    base.OnModelCreating(builder); 

builder.Entity<MyUser>() 
    .HasMany(e => e.Claims) 
    .WithOne() 
    .HasForeignKey(e => e.UserId) 
    .IsRequired() 
    .OnDelete(DeleteBehavior.Cascade); 

builder.Entity<MyUser>() 
    .HasMany(e => e.Logins) 
    .WithOne() 
    .HasForeignKey(e => e.UserId) 
    .IsRequired() 
    .OnDelete(DeleteBehavior.Cascade); 

builder.Entity<MyUser>() 
    .HasMany(e => e.Roles) 
    .WithOne() 
    .HasForeignKey(e => e.UserId) 
    .IsRequired() 
    .OnDelete(DeleteBehavior.Cascade); 
} 

Migrating Authentication and Identity to ASP.NET Core 2.0

Các vấn đề liên quan