NopCommerce4.2 常见错误及异常处理 - Lazy-loaded navigation properties must have backing fields
An error occurred while starting the application.
Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateFieldMapping(IModel model)
InvalidOperationException: No field was found backing property 'WeiActivityWinInfos' of entity type 'WeiActivityInfo'. Lazy-loaded navigation properties must have backing fields. Either name the backing field so that it is picked up by convention or configure the backing field to use.
出现以上问题,主要是导航属性命名不规范导致的
错误代码
public partial class WeiActivityInfo : BaseEntity
{
private ICollection<WeiActivityWinInfo> _weiActivityWinInfo;
public virtual ICollection<WeiActivityWinInfo> WeiActivityWinInfos
{
get { return _weiActivityWinInfo?? (_weiActivityWinInfo= new List<WeiActivityWinInfo>()); }
protected set { _weiActivityWinInfo= value; }
}
}
EF 导航配置命名上要保持一致,否则可能到这个错误,正确的配置,保证
private与public 属性集合名称一致!
public partial class WeiActivityInfo : BaseEntity
{
private ICollection<WeiActivityWinInfo> _weiActivityWinInfos;
public virtual ICollection<WeiActivityWinInfo> WeiActivityWinInfos
{
get { return _weiActivityWinInfos?? (_weiActivityWinInfos= new List<WeiActivityWinInfo>()); }
protected set { _weiActivityWinInfos= value; }
}
}