[英]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.