简体   繁体   English

ApplicationUser 始终为空 ASP.NET CORE 3.0 标识

[英]ApplicationUser always null ASP.NET CORE 3.0 Identity

When I try to get my users från the table AspNetUsers the users always return null even though there is a user in the table.当我尝试从表 AspNetUsers 获取我的用户时,即使表中有用户,用户也总是返回 null。

public class ApplicationUser : IdentityUser
{
    public int Rating { get; set; }
    public string ProfileImageUrl { get; set; }
    public DateTime MemberSince { get; set; }
    public bool IsActive { get; set; }
}

What I want to show is Posts with Author information我想显示的是带有作者信息的帖子

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime Created { get; set; }

    public virtual Foorum Forum { get; set; }
    public virtual IEnumerable<PostReply> PostReplies { get; set; }
    public virtual ApplicationUser User { get; set; }
}

And this is how I'm getting Posts and Users and User is always null here.这就是我获取帖子和用户的方式,用户在这里始终为空。

 public Foorum GetById(int id)
    {
        var forum = _forumDbContext.Forums.Where(f => f.Id == id)
            .Include(f => f.Posts)
            .ThenInclude(p => p.User)
            .Include(f => f.Posts)
            .ThenInclude(p => p.PostReplies)
            .ThenInclude(r => r.User)
            .FirstOrDefault();

        return forum;
    }

This is how my DbContext looks like:这是我的 DbContext 的样子:

    public class ForumDbContext : IdentityDbContext<IdentityUser>
{
    public ForumDbContext(DbContextOptions<ForumDbContext> options)
        : base(options)
    {
    }

    public DbSet<Foorum> Forums { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<PostReply> PostReplies { get; set; }
}

Startup.cs启动文件

        public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ForumDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ForumDbContext>();
}

And I've also added app.UseAuthentication();我还添加了 app.UseAuthentication(); app.UseAuthorization(); app.UseAuthorization(); in the Configure method.在配置方法中。

What am I missing here, why can't I get users from the database?我在这里错过了什么,为什么我不能从数据库中获取用户?

UPDATE更新

Change IdentityUser to ApplicationUser in Startup.cs & ForumDbContext.cs files, it may help.Startup.csForumDbContext.cs文件ForumDbContext.cs IdentityUser更改为ApplicationUser ,这可能会有所帮助。


As I mentioned in this post , you should add a property UserId and just set it to the desired User Id such as _currentUser.Id .正如我在这篇文章中提到,您应该添加一个属性UserId并将其设置为所需的User Id例如_currentUser.Id The entity framework will include User into your model automatically.实体框架会自动将User包含到您的模型中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core Identity不会注入UserManager <ApplicationUser> - ASP.NET Core Identity does not inject UserManager<ApplicationUser> IFormFile 在 ASP.NET Core 3.0 MVC 中始终为 NULL - IFormFile is always NULL in ASP.NET Core 3.0 MVC ApplicationUser上的ASP.net Core 1.0映射属性返回null - ASP.net Core 1.0 Mapping property on ApplicationUser returning null 在ASP.NET Core MVC中注入ApplicationUser - Inject ApplicationUser in ASP.NET Core MVC ASP.NET Core Identity ApplicationUser 与域模型实体关系的分离 - Separation of ASP.NET Core Identity ApplicationUser with relationship to Domain Model Entity ASP.NET Core 标识用户管理器<ApplicationUser> -Error 没有为此对象定义无参数构造函数 - ASP.NET Core Identity UserManager<ApplicationUser> -Error No parameterless constructor defined for this object ASP.NET 核心 - 参数 1:无法从 'long' 转换为 '.Identity.ApplicationUser' - ASP.NET Core - Argument 1: cannot convert from 'long' to '.Identity.ApplicationUser' Asp.Net Core 3.0 MVC Identity 添加角色问题 - Asp.Net Core 3.0 MVC Identity Adding Role Problem 身份 asp.net 核心 3.0 - 未找到 IdentityDbContext - Identity asp.net core 3.0 - IdentityDbContext not found ASP.NET Core 3.0 将身份用户分配给自定义 model - ASP.NET Core 3.0 Assign Identity User to custom model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM