我有这个类。
在 DbContext 类中,我已经这样做了。
现在,当我尝试使用以下查询语句来查询我的存储库中的
我认为它的返回类型是
我应该怎么做才能使查询
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.
时智能提示选项并没有显示来自ApplicationUser
的字段。我应该怎么做才能使查询
Users
表返回ApplicationUser
类型,并且为什么它给出的返回类型是IdentityUser
?
_dbContext.Users.OfType<ApplicationUser>().SingleOrDefault...
吗? - Raphaël Althaus.OfType
不是它应该的方式。(我不需要那样做)- 尝试删除modelBuilder.Entity<IdentityUser>().ToTable("Users");
,因为您已将ApplicationUser映射到该表。我的怀疑是第二行某种方式覆盖了第一行,这就是为什么该实体返回IdentityUser
而不是ApplicationUser
。请尝试一下。 - Marco