EF6 does not lazy load navigation property(EF6 不延迟加载导航属性)
问题描述
我遇到了 EF6 延迟加载的问题.我搜索了 StackOverflow,但我发现的其他问题不适合我的情况.
I'm having an issue with EF6 lazy loading. I've searched StackOverflow, but the other questions I've found doesn't fit my case.
我正在使用 virtual
关键字,并且我的类是 public
.LazyLoadingEnabled
和 ProxyCreationEnabled
都设置为 true
.
I'm using the virtual
keyword and my classes are public
. LazyLoadingEnabled
and ProxyCreationEnabled
are both set to true
.
当我从数据库加载 course
对象时,presentationId
被设置为正确的 id
和 presentation
是 null
是正确的,因为它还没有被加载.
When I load a course
object from the db, presentationId
is set to the correct id
and presentation
is null
which is correct, because it hasn't been loaded yet.
当我将 presentation
属性传递给 PresentationsController.ToDto()
方法时,它应该是延迟加载的,但我得到了一个 null 引用
方法内部的异常,因为它仍然是 null
.
When I pass the presentation
property to the PresentationsController.ToDto()
method it should be lazy loaded, but I get a null reference
exception inside the method because it's still null
.
我知道这些关系正在起作用,因为当我在 Watch window
中强制加载 course
的 presentation
属性时,会出现断点在 public static CourseDto ToDto(Course item, DnbContext db)
方法中,它被加载.查看图片:
I know that the relationships are working because when I force load the presentation
property of a course
in the Watch window
with a break point at the public static CourseDto ToDto(Course item, DnbContext db)
method it gets loaded. See images:
你可以看到 item.presentation
是 null
:
当我手动评估引用与 item
对象相同的演示文稿的 db.courses.Find(257).presentation
时,它们都已加载:
When I manually evaluate db.courses.Find(257).presentation
which is referencing the same presentation as the item
object does, they're both loaded:
这是我的 POCO:
public abstract class BaseModel : ISoftDelete {
public int id { get; set; }
}
public class Course : BaseModel {
[Required]
public int presentationId { get; set; }
public virtual Presentation presentation { get; set; }
}
我的 Web API 控制器方法:
My Web API controller methods:
// GET api/Courses/5
public CourseDto GetCourse(int id) {
var item = db.courses.FirstOrDefault(x => x.id == id);
return ToDto(item, db);
}
public static CourseDto ToDto(Course item, DnbContext db) {
var dto = new CourseDto();
if (item.presentationId > 0) dto.presentation = PresentationsController.ToDto(item.presentation, db);
return dto;
}
有什么想法吗?
推荐答案
如果你想通过动态代理使用延迟加载,实体必须明确声明公共构造函数.(如果你有其他带参数的)
Entities must have explicitly declared public constructors if you want to use lazy loading via dynamic proxies. (If you have other with parameters)
public abstract class BaseModel : ISoftDelete {
public BaseModel() { }
public int id { get; set; }
}
public class Course : BaseModel {
public Course() { }
[Required]
public int presentationId { get; set; }
public virtual Presentation presentation { get; set; }
}
这篇关于EF6 不延迟加载导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:EF6 不延迟加载导航属性
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01