繁体   English   中英

延迟加载EF Core 2.1无法使用Identity?

[英]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:'实体类型'ApplicationUser'上的导航属性'RefMedicalSpecialty'不是虚拟的。 UseLazyLoadingProxies要求所有实体类型都是公共的,未密封的,具有虚拟导航属性,并且具有公共或受保护的构造函数。

Entity Framework Core 2.1引入了延迟加载。 它要求所有导航属性都是虚拟的,如问题延迟加载代理中所述:允许指定实体类型/导航

目前,当使用延迟加载代理时,模型中的每个实体类型必须适合代理,并且所有导航必须是虚拟的。 此问题是关于允许某些实体类型/导航延迟加载而其他实体类型/导航不是。

问题仍然存在且无法解决问题,因此仍不支持您所需的方案。

而且例外情况告诉你:

UseLazyLoadingProxies要求所有实体类型都是公共的,未密封的,具有虚拟导航属性,并且具有公共或受保护的构造函数。

因此,将所有导航属性(即引用其他实体的属性)更改为virtual

或者使用ILazyLoader如在没有代理的延迟加载中所述

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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM