简体   繁体   中英

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.

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.

[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 . UploadedBy is the problem which is mapped to ApplicationUser.

With an admin role, this never fails and it properly maps to the ApplicationUser so CreatedBy is never 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.

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 .

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 .

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. And again, if I sign in with an admin account, CreatedBy is never null.

Thanks to @TanvirArjel for comment to original question, I was able to follow the trail and find the issue. It was in the include logic within the form factory.

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);


        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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