我正在研究EF Core
/ Explicitly
几乎没有属性的Lazy loaded
API。但这在我尝试时会抛出一些异常,
型号:
public int NationalityId { get; set; }
[NotMapped]
public Country Nationality { get; set; }
回购:
RepositoryContext.Entry(user).Reference(x => x.Nationality).Load();
这使我"Property Nationality cannot be found on the entity User"
还尝试了virtual
而不是[NotMapped]
,
modelBuilder.Entity<User>().Ignore(x => x.Nationality);
当我删除"[NotMapped]"
属性并尝试运行时,出现以下错误,
"Column name NationalityId1 cannot be found" ==> SQLException
对于延迟加载也会发生同样的情况-我使用ILazyLoader
并且尝试过,
型号:
[NotMapped]
public Country Nationality
{
get => LazyLoader?.Load(this, ref _nationality);
set => _nationality = value;
}
有人可以帮助我解决问题并同时使用两种数据加载技术吗?
注意:我正在使用EFCore 2.2.4
答案 0 :(得分:0)
您应该教EF与其相关的实体,而不要忽略/ NotMap相关的实体。当您使用DataAnottation属性时。您可以使用ForeignKeyAttribute
[ForeignKey("Nationality")]
public int NationalityId { get; set; }
public Country Nationality { get; set; }