How do I get the fields in User to map to LookUpDetail using EF Code First? (preferably using annotations, not FluentAPI)
public class User
{
public int? ProvStateId { get; set; }
public LookUpDetail ProvState { get; set; }
public int? CountryId { get; set; }
public LookUpDetail Country { get; set; }
}
public class LookUpDetail
{
public int LookUpDetailId { get; set; }
public string Value { get; set; }
}
I want both ProvStateId and CountryId to map to LookUpDetailId, and be able to populate ProvState and COuntry when pulling a User record.
I’ve tried
[ForeignKey("ProvStateId")]
public LookUpDetail ProvState { get; set; }
And that seems to set the field in the DB, but
LookUpDetail ProvState
is always null when I try to pull a User record.
The LookUpDetail table is populated and I am able to display it’s contents just fine.
What am I missing?
You need to make the Model class properties to be virtual property that will allow EF framework to create proxy and do load related object for you once you do lazy loading.
(I am not sure you do lazy loading or not, if not you need to do explicit load related object)
try this….