Core 1.0 Mapping property on ApplicationUser returning null c# entity-framework-core


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:

public class AccountDetail
    public int Id { get; set; }
    public ICollection<FileUpload> Files { get; set; }


The problem is occurring in the Files property.

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 }"

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


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.

9/7/2019 9:18:39 PM

Accepted Answer

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

1/28/2019 2:42:16 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow