无法从ASP.NET Core 2.0中的IdentityUser和IdentityRoleinheritance
我正在尝试更新到.NET Core 2.0,但会弹出几个错误:
问题:
我有两个类,ApplicationRole和ApplicationUser,它inheritanceIdentityRole和IdentityUser的一些属性。
随着Core 2.0的更新,我收到以下错误,声称无法找到IdentityRole和IdentityUser。
但是, Microsoft.AspNetCore.Identity.EntityFrameworkCore 2.0
软件包安装在新版本的.NET Core中
关于IdentityRole:
消息1:
找不到类型或命名空间名称’IdentityRole’(是否缺少using指令或程序集引用?)
消息2:
‘Application.Models.ApplicationRole’类型不能用作generics类型或方法’IdentityDbContext’中’TRole’类型的参数。 没有从“Application.Models.ApplicationRole”到“Microsoft.AspNetCore.Identity.IdentityRole”的隐式引用转换。
ApplicationRole的定义:
public class ApplicationRole:IdentityRole { public string Description { get; set; } public DateTime CreatedDated { get; set; } public string IPAddress { get; set; } }
关于IdentityUser:
消息1:
找不到类型或命名空间名称’IdentityUser’(是否缺少using指令或程序集引用?)
消息2:
‘Application.Models.ApplicationUser’类型不能用作generics类型或方法’IdentityDbContext’中’TUser’类型的参数。 没有从“Application.Models.ApplicationUser”到“Microsoft.AspNetCore.Identity.IdentityUser”的隐式引用转换。
ApplicationUser的定义
public class ApplicationUser:IdentityUser { public string Name { get; set; } }
下面的指南定义了这些类及其用法如下:
https://www.c-sharpcorner.com/article/asp-net-core-mvc-authentication-and-role-based-authorization-with-asp-net-core/
随着Core 2.0的改变,我想知道IdentityRole和IdentityUser是如何改变的,所以我不能再inheritance它们了。
解决了。
保持那些类Microsoft.AspNetCore.Identity.EntityFrameworkCore
更改的包。 要访问这些clases( IdentityUser
和IdentityRole
),必须添加
using Microsoft.AspNetCore.Identity;
有了这个,问题就消失了。
ICollection
, ICollection
和ICollection
导航属性已从Microsoft.AspNetCore.Identity.IdentityUser
删除。
您应该手动定义它们
public class MyUser : IdentityUser { public virtual ICollection> Roles { get; } = new List>(); public virtual ICollection> Claims { get; } = new List>(); public virtual ICollection> Logins { get; } = new List>(); }
要在运行EF Core Migrations时防止重复的外键,请将以下内容添加到IdentityDbContext
类的OnModelCreating
方法中
protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity() .HasMany(e => e.Claims) .WithOne() .HasForeignKey(e => e.UserId) .IsRequired() .OnDelete(DeleteBehavior.Cascade); builder.Entity () .HasMany(e => e.Logins) .WithOne() .HasForeignKey(e => e.UserId) .IsRequired() .OnDelete(DeleteBehavior.Cascade); builder.Entity () .HasMany(e => e.Roles) .WithOne() .HasForeignKey(e => e.UserId) .IsRequired() .OnDelete(DeleteBehavior.Cascade); }
将身份validation和身份迁移到ASP.NET Core 2.0
这发生在我身上,但我使用的是Asp.net Core和传统的.NET Framework。 您需要添加Nuget包Microsoft.Extensions.Identity
。 它包含IdentityUser
和IdentityRole
类。
我认为你必须添加这个的原因是你的项目没有使用Microsoft.AspNetCore.All包。
上述就是C#学习教程:无法从ASP.NET Core 2.0中的IdentityUser和IdentityRoleinheritance分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1009626.html