简体   繁体   English

ApplicationUser上的ASP.net Core 1.0映射属性返回null

[英]ASP.net Core 1.0 Mapping property on ApplicationUser returning null

First, I adopted a project due to a developer jumping ship, so not only is this my first experience with .net core, but I'm having to digest a large code-base that I didn't write. 首先,由于开发人员的跳槽,我采用了一个项目,所以这不仅是我对.net core的首次体验,而且我必须消化一个我没有编写的大型代码库。

I need to know where to look to resolve an issue. 我需要知道在哪里解决问题。 I have a view that renders in a table a list of records. 我有一个视图,该视图在表中呈现记录列表。

Here are the relevant classes accessed: 以下是访问的相关类:

[Table("AccountDetails")]
public class AccountDetail
{
    public int Id { get; set; }
    public ICollection<FileUpload> Files { get; set; }

}

The problem is occurring in the Files property. 该问题发生在Files属性中。

[Table("FileUploads")]
public class FileUpload
{
    public int Id { get; set; }
    public string FileName { get; set; }
    public ApplicationUser CreatedBy { get; set; }
    public string ContentType { get; set; }
    public byte[] Content { get; set; }
}

Here's the method that returns this list: 这是返回此列表的方法:

var files = _form.AccountDetail.Files.Select(q => new FileModel
  {
    FileName = q.FileName,
    FileId = q.Id,
    EffectiveDate = q.CreatedOn.ToString("d"),
    FormId = _form.Id,
    UploadedBy = $"{ q.CreatedBy.FirstName } { q.CreatedBy.LastName }"
  }).ToList();

ApplicationUser extends IdentityUser . ApplicationUser扩展了IdentityUser UploadedBy is the problem which is mapped to ApplicationUser. UploadedBy是映射到ApplicationUser的问题。

With an admin role, this never fails and it properly maps to the ApplicationUser so CreatedBy is never null . 使用管理员角色,此操作永远不会失败,并且可以正确映射到ApplicationUser因此CreatedBy永远不会为null

However, under a different user account which is not an admin role anything not created by that user returns null, which triggers an object reference error. 但是,在不是管理员角色的其他用户帐户下,该用户未创建的任何内容都将返回null,这将触发对象引用错误。

Now I know the issue deserves a quick response like, "Hey, obviously the user doesn't have the right permissions.", but I need assistance with where to look to solve this. 现在,我知道该问题应该得到快速响应,例如,“嘿,显然用户没有正确的权限。”,但是我需要在哪里寻找解决方案的帮助。

In this case, CreatedBy should never return null . 在这种情况下, CreatedBy应该永远不会返回null

I don't quite know where or how this auto-mapping is occurring. 我不知道这种自动映射在哪里或如何发生。

I found this in snapshot: 我在快照中找到了这个:

modelBuilder.Entity("Accounting.Entities.FileUpload", b =>
            {
                b.HasOne("Accounting.Entities.ApplicationUser", "CreatedBy")
                    .WithMany()
                    .HasForeignKey("CreatedById");


            });

CreatedById is the column in the actual table that references AspNetUsers . CreatedById是实际表中引用AspNetUsers的列。

If I go all the way back to the constructor for the controller being called: 如果我一直回到被称为控制器的构造函数:

public FormAccountsController(ApplicationDbContext context,
                           UserManager<ApplicationUser> userManager,
                           IMapper mapper,
                           RoleManager<IdentityRole> roleManager) : base(context, userManager, roleManager, mapper)
    {

    }

And I look in ApplicationDbContext context , I can see FileUploads and CreatedBy is null for the record that was not created by this particular user, but from this point on I'm not sure where to look. ApplicationDbContext context ,我看到该特定用户未创建的记录的FileUploads和CreatedBy为null,但是从现在开始,我不确定在哪里查找。 And again, if I sign in with an admin account, CreatedBy is never null. 再一次,如果我使用管理员帐户登录,则CreatedBy永远不会为null。

Thanks to @TanvirArjel for comment to original question, I was able to follow the trail and find the issue. 感谢@TanvirArjel对原始问题的评论,我得以按照追踪发现问题。 It was in the include logic within the form factory. 它在表单工厂内的include逻辑中。

if (user.AccountProfile != null)
        {
            form = _context.Forms.Include(q => q.AccountingDetail)
                .ThenInclude(q => q.Files)


                .ThenInclude(q => q.CreatedBy) // This was missing
                .Single(q => q.Id == formId);


        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM