我有这门课
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; }
....
}
现在,当我尝试使用类似的东西查询存储库中的 Users
表时
var user = _db.Users.SingleOrDefault(u=>u.anyfield);
我认为它的返回类型为 IdentityUser
而不是 ApplicationUser
因为当我这样做时 u=>u.
intellisense 选项不显示来自 ApplicationUser
我应该怎么做才能使 Users
表中的查询返回 ApplicationUser
类型,为什么它给我的返回类型为 IdentityUser
最佳答案
因为IdentityDbContext不是你定义的类,而ApplicationUser是你定义的类。您创建从 IdentityUser 继承的 ApplicationUser。您创建一个继承自 IdentityDbContext 的上下文。 IdentityDbContext 公开 DbSet。
如果您希望数据作为 ApplicationUser 返回,您可以执行以下操作:
context.Users.OfType<ApplicationUser>();
您也可以在您的自定义 DbContext 中执行此操作(未验证):
public new DbSet<ApplicationUser> Users { get; set; }
虽然我还没有测试过。
关于c# - 为什么返回类型是 IdentityUser 而不是 ApplicationUser?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22225365/