EF6不延迟加载导航属性 [英] EF6 does not lazy load navigation property
问题描述
我在与EF6延迟加载的问题。我搜索计算器,但我发现的其他问题不适合我的情况。
我使用了虚拟
关键字和我的班公共
。 LazyLoadingEnabled
和 ProxyCreationEnabled
均设置为真正
。从DB p>
当我打开一个当然
对象, presentationId
被设置为正确的 ID
和演示
是空
这是正确的,因为它没有被加载。
当我通过演示
属性设置为 PresentationsController.ToDto()
方法,它应该是懒加载状态,但我得到的方法在空引用
例外,因为它仍然空
。
我知道,关系工作,因为当我强迫加载演示
在<$ C $一个当然
在监视窗口
以一个破发点的属性C>公共静态CourseDto ToDto(课程项目,DnbContext DB)方法,它被加载。见图片:
正如你所看到 item.presentation
是空
:
当我手动评估 db.courses.Find(257).presentation
这是引用相同的演示文稿作为项目
对象呢,他们都装:
下面是我的波苏斯:
公共抽象类BaseModel:ISoftDelete {
公众诠释ID {搞定;组; }
}
公共类课程:BaseModel {
[必填]
公众诠释presentationId {搞定;组; }
公共虚拟介绍介绍{搞定;组; }
}
我的Web API控制器方法:
//获取API /课程/ 5
公共CourseDto GetCourse(INT ID){
VAR项目= db.courses.FirstOrDefault(X = > x.id == ID);
返回ToDto(项目,DB);
}
公共静态CourseDto ToDto(课程项目,DnbContext DB){
变种DTO =新CourseDto();
如果(item.presentationId大于0)dto.presentation = PresentationsController.ToDto(item.presentation,DB);
返回DTO;
}
任何想法?
实体必须明确宣布公共构造如果要通过动态代理使用延迟加载。 (如果您有其他带参数)
公共抽象类BaseModel:ISoftDelete {
公共BaseModel(){}
酒店的公共INT编号{搞定;组; }
}
公共类课程:BaseModel {
公共课程(){}
[必填]
公众诠释presentationId {搞定;组; }
公共虚拟介绍介绍{搞定;组; }
}
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.
I'm using the virtual
keyword and my classes are public
. LazyLoadingEnabled
and ProxyCreationEnabled
are both set to true
.
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.
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
.
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:
As you can see item.presentation
is null
:
When I manually evaluate db.courses.Find(257).presentation
which is referencing the same presentation as the item
object does, they're both loaded:
Here are my POCOs:
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; }
}
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;
}
Any ideas?
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不延迟加载导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!