延迟加载 EF Core 2.1 不适用于身份?
Lazy loading EF Core 2.1 not working with Identity?
我按照这里的例子
https://docs.microsoft.com/en-us/ef/core/querying/related-data#lazy-loading
我的两个 类 看起来像这样
public class RefMedSchool
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public virtual ICollection<ApplicationUser> ApplicationUser { get; set; }
}
public class ApplicationUser : IdentityUser
{
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public bool MustChangePassword { get; set; }
public int? MedicalSpecialtyId { get; set; }
[ForeignKey("RefMedicalSpecialtyForeignKey")]
public RefMedicalSpecialty RefMedicalSpecialty { get; set; }
public int RefMedSchoolId { get; set; }
public virtual RefMedSchool RefMedSchool { get; set; }
public UserProfileData UserProfileData { get; set; }
public ICollection<UserFeedback> UserFeedbacks { get; set; }
public ICollection<UserAction> UserActions { get; set; }
public ICollection<UserProgram> UserPrograms { get; set; }
}
但是当尝试创建数据库时,出现以下错误。怎么了 ?这些属性根据需要是虚拟的。
System.InvalidOperationException: 'Navigation property 'RefMedicalSpecialty' on entity type 'ApplicationUser' is not virtual. UseLazyLoadingProxies requires all entity types to be public, unsealed, have virtual navigation properties, and have a public or protected constructor.'
Entity Framework Core 2.1 引入了延迟加载。它要求 所有 导航属性都是虚拟的,如问题 Lazy-loading proxies: allow entity types/navigations to be specified:
中所述
Currently when lazy-loading proxies are used every entity type in the model must be suitable to proxy and all navigations must be virtual. This issue is about allowing some entity types/navigations to be lazy-loaded while others are not.
问题仍未解决且未解决,因此仍不支持您想要的方案。
正如异常告诉您的那样:
UseLazyLoadingProxies requires all entity types to be public, unsealed, have virtual navigation properties, and have a public or protected constructor.
因此,将所有导航属性(即引用其他实体的属性)更改为 virtual
。
或使用 ILazyLoader
,如 Lazy-loading without proxies 中所述:
public class Blog
{
private ICollection<Post> _posts;
public Blog()
{
}
private Blog(ILazyLoader lazyLoader)
{
LazyLoader = lazyLoader;
}
private ILazyLoader LazyLoader { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Post> Posts
{
get => LazyLoader?.Load(this, ref _posts);
set => _posts = value;
}
}
我按照这里的例子
https://docs.microsoft.com/en-us/ef/core/querying/related-data#lazy-loading
我的两个 类 看起来像这样
public class RefMedSchool
{
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public virtual ICollection<ApplicationUser> ApplicationUser { get; set; }
}
public class ApplicationUser : IdentityUser
{
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public bool MustChangePassword { get; set; }
public int? MedicalSpecialtyId { get; set; }
[ForeignKey("RefMedicalSpecialtyForeignKey")]
public RefMedicalSpecialty RefMedicalSpecialty { get; set; }
public int RefMedSchoolId { get; set; }
public virtual RefMedSchool RefMedSchool { get; set; }
public UserProfileData UserProfileData { get; set; }
public ICollection<UserFeedback> UserFeedbacks { get; set; }
public ICollection<UserAction> UserActions { get; set; }
public ICollection<UserProgram> UserPrograms { get; set; }
}
但是当尝试创建数据库时,出现以下错误。怎么了 ?这些属性根据需要是虚拟的。
System.InvalidOperationException: 'Navigation property 'RefMedicalSpecialty' on entity type 'ApplicationUser' is not virtual. UseLazyLoadingProxies requires all entity types to be public, unsealed, have virtual navigation properties, and have a public or protected constructor.'
Entity Framework Core 2.1 引入了延迟加载。它要求 所有 导航属性都是虚拟的,如问题 Lazy-loading proxies: allow entity types/navigations to be specified:
中所述Currently when lazy-loading proxies are used every entity type in the model must be suitable to proxy and all navigations must be virtual. This issue is about allowing some entity types/navigations to be lazy-loaded while others are not.
问题仍未解决且未解决,因此仍不支持您想要的方案。
正如异常告诉您的那样:
UseLazyLoadingProxies requires all entity types to be public, unsealed, have virtual navigation properties, and have a public or protected constructor.
因此,将所有导航属性(即引用其他实体的属性)更改为 virtual
。
或使用 ILazyLoader
,如 Lazy-loading without proxies 中所述:
public class Blog
{
private ICollection<Post> _posts;
public Blog()
{
}
private Blog(ILazyLoader lazyLoader)
{
LazyLoader = lazyLoader;
}
private ILazyLoader LazyLoader { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Post> Posts
{
get => LazyLoader?.Load(this, ref _posts);
set => _posts = value;
}
}