gpt4 book ai didi

c# - 延迟加载 EF Core 2.1 不适用于身份?

转载 作者:太空宇宙 更新时间:2023-11-03 20:55:45 24 4
gpt4 key购买 nike

我按照这里的例子

https://learn.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.

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

或使用 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;
}
}

关于c# - 延迟加载 EF Core 2.1 不适用于身份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50722475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com