Search code examples
ef-code-firstpocoasp.net-identity

Asp.net-identity ApplicationUser property on POCO loads as null


I have a model

public class BaseCave

    public string OldSasaCode { get; set; }
    [Display(Name = "Cave Name")]
    public string Name { get; set; }
    public string Description { get; set; }
    public double Longitude { get; set; }
    public double Latitude { get; set; }
    public DateTime LastEditDate { get; set; }
    public ApplicationUser LastEditMember { get; set; }
    public bool IsDeleted {get; set;}
}

public class Cave: BaseCave
{
    public int CaveID { get; set; }
}

and i use this on ApplicationDbContext created by ASP.NET-Identiy. I set the LastEditMember property equals to the current logged in member and save.

In the DB the last editmember's ID is Saved in my Cave table, but when i load the Cave entity, the LastEditMember property is null, eventhough the ID for the user was sucsesfully saved in the DB.


Solution

  • any Related Entities must be marked as virtual for entity framework to be able to use them.

    This also indicates to Entity Framework that it is a related Entity.

    So in this example it should have been:

    public virtual ApplicationUser LastEditMember { get; set; }