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; }