简体   繁体   English

为什么返回类型为IdentityUser而不是ApplicationUser?

[英]Why is the return type IdentityUser and not ApplicationUser?

I have this class 我有这门课

public class ApplicationUser : IdentityUser
{
   public string Email { get; set; }
   public string ConfirmationToken { get; set; }
   public bool IsConfirmed { get; set; }
   ...
}

and in DbContext class this is what I've done DbContext类中,这就是我所做的

public partial class PickerDbContext : IdentityDbContext
{
    public PickerDbContext():base("DefaultConnection")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<ApplicationUser>().ToTable("Users");
        modelBuilder.Entity<IdentityUser>().ToTable("Users");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
    }
    //public DbSet<License> Licenses { get; set; }
    ....
}

now when I try to query the Users table in my repository with something like 现在当我尝试用类似的东西查询我的存储库中的Users表时

var user = _db.Users.SingleOrDefault(u=>u.anyfield);

I think it has return type as IdentityUser and not ApplicationUser because when I do u=>u. 我认为它的返回类型为IdentityUser而不是ApplicationUser因为当我执行u=>u. intellisense option doesn't show the fields from ApplicationUser intellisense选项不显示ApplicationUser的字段

在此输入图像描述

What should I do to make the querying in Users table return ApplicationUser type and why is it giving me return type as IdentityUser 我该怎么做才能在Users表中查询返回ApplicationUser类型,为什么它给我返回类型为IdentityUser

Because IdentityDbContext is not a class you defined, and ApplicationUser is a class that you did define. 因为IdentityDbContext不是您定义的类,ApplicationUser是您定义的类。 You create ApplicationUser that inherits from IdentityUser. 您创建继承自IdentityUser的ApplicationUser。 You create a context that inherits from IdentityDbContext. 您创建一个继承自IdentityDbContext的上下文。 IdentityDbContext exposes DbSet. IdentityDbContext公开DbSet。

If you want the data to come back as ApplicationUser you can do the following: 如果您希望数据作为ApplicationUser返回,您可以执行以下操作:

context.Users.OfType<ApplicationUser>();

You may also be able to do this in your custom DbContext (unverified): 您也可以在自定义DbContext(未验证)中执行此操作:

public new DbSet<ApplicationUser> Users { get; set; }

I haven't tested that though. 我没有测试过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有从ApplicationUser到IdentityUser的隐式引用转换 - There is no implicit reference conversion from ApplicationUser to IdentityUser 从ApplicationUser(IdentityUser)继承的类上的级联删除 - Cascade delete on classes inherited from ApplicationUser(IdentityUser) 当我使用 ApplicationUser 而不是 IdentityUser 时,出现此错误“无法解析 AspNetCore.Identity.SignInManager 类型的服务” - I got this error "Unable to resolve service for type AspNetCore.Identity.SignInManager" when I use ApplicationUser instead IdentityUser 类型“IdentityUser”在未引用的程序集中定义 - The type 'IdentityUser' is defined in an assembly that is not referenced 使用命名类型(IdentityUser)时出错,将其分配给var时工作正常,为什么? - Error when using named type (IdentityUser), works fine when assigning it to var, why? ASP.net身份:如何获取当前的IdentityUser(ApplicationUser)? UserManager.FindById在哪里? - ASP.net identity: How to get current IdentityUser (ApplicationUser)? Where is UserManager.FindById? .NET 6 身份自定义 - 错误:没有从 ApplicationUser 到 IdentityUser 的隐式引用转换<string></string> - .NET 6 Identity Customization - Error: There is no implicit reference conversion from ApplicationUser to IdentityUser<string> 没有从 ApplicationUser 到 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的隐式引用转换 - There is no implicit reference conversion from ApplicationUser to Microsoft.AspNet.Identity.EntityFramework.IdentityUser 使用 ApplicationUser 时出现问题:IdentityUser, UserManager in clean architecture asp.net core - Trouble when using ApplicationUser: IdentityUser, UserManager in clean architecture asp.net core 找不到类型命名空间名称IdentityUser - The type namespace name IdentityUser could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM