Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么返回类型是IdentityUser而不是ApplicationUser?_C#_Asp.net Mvc_Entity Framework_Asp.net Mvc 5_Asp.net Identity - Fatal编程技术网

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

C# 为什么返回类型是IdentityUser而不是ApplicationUser?,c#,asp.net-mvc,entity-framework,asp.net-mvc-5,asp.net-identity,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,Asp.net Identity,我有这门课 public class ApplicationUser : IdentityUser { public string Email { get; set; } public string ConfirmationToken { get; set; } public bool IsConfirmed { get; set; } ... } 在DbContext课程中,这就是我所做的 public partial class PickerDbContext :

我有这门课

public class ApplicationUser : IdentityUser
{
   public string Email { get; set; }
   public string ConfirmationToken { get; set; }
   public bool IsConfirmed { get; set; }
   ...
}
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; }
    ....
}
我认为它的返回类型是
IdentityUser
而不是
ApplicationUser
,因为当我执行
u=>u时,
intellisense选项不会显示
ApplicationUser


我应该怎么做才能使
Users
表中的查询返回
ApplicationUser
类型,为什么返回类型为
IdentityUser

,因为IdentityDbContext不是您定义的类,而ApplicationUser是您定义的类。创建从IdentityUser继承的ApplicationUser。创建从IdentityDbContext继承的上下文。IdentityDbContext公开DbSet

如果希望数据作为应用程序用户返回,可以执行以下操作:

context.Users.OfType<ApplicationUser>();
context.Users.OfType();
您也可以在自定义DbContext中执行此操作(未验证):

public新DbSet用户{get;set;}

不过我还没有测试过。

您是否尝试过
\u dbContext.Users.OfType().SingleOrDefault…
没有,我会试一试。只要一分钟!这似乎奏效了。我不知道。另一个猜测,因为使用
。of type
不是它应该的方式。(我不需要这样做)-尝试删除
modelBuilder.Entity().ToTable(“用户”)因为您已经将ApplicationUser映射到该表。我的怀疑是,第二行以某种方式覆盖了第一行,这就是为什么该实体返回
IdentityUser
,而不是
ApplicationUser
。请试一试。@Serv ok将试一试
context.Users.OfType<ApplicationUser>();
public new DbSet<ApplicationUser> Users { get; set; }