EF6不会延迟加载导航属性 [英] EF6 does not lazy load navigation property

查看:197
本文介绍了EF6不会延迟加载导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EF6懒加载的问题。我搜索了StackOverflow,但是我发现的其他问题并不适合我的情况。



我正在使用 virtual 关键字和我的类是 public LazyLoadingEnabled ProxyCreationEnabled 都设置为 true p>

当我从数据库加载课程对象时, presentationId 被设置为正确的 id 演示文稿 null 是正确的,因为它还没有加载。



当我将演示文稿属性传递给 PresentationsController.ToDto()方法应该是延迟加载,但是我在方法中得到一个 null引用异常,因为它仍然是 null



我知道这些关系是有效的,因为当我强制加载在$ code code code code code code code code code code code code> public static CourseDto ToDto(Course item,DnbContext db)方法它被加载。见图:



如您所见, item.presentation null





当我手动评估 db.courses.Find(257).presentation 时,它引用与 item 对象,它们都被加载:





这是我的POCO:

  public abstract class BaseModel:ISoftDelete {
public int id {get;组;
}

public class课程:BaseModel {
[必需]
public int presentationId {get;组; }
public virtual Presentation presentation {get;组;
}

我的Web API控制器方法:

  // GET api / Courses / 5 
public CourseDto GetCourse(int id){
var item = db.courses.FirstOrDefault(x = > x.id == id);
返回ToDto(item,db);
}

public static CourseDto ToDto(课程项目,DnbContext db){
var dto = new CourseDto();

if(item.presentationId> 0)dto.presentation = PresentationsController.ToDto(item.presentation,db);

return dto;
}

任何想法?

解决方案

如果要通过动态代理使用延迟加载,实体必须明确声明为公共构造函数。 (如果你有其他参数)

  public abstract class BaseModel:ISoftDelete {
public BaseModel(){}
public int id {get;组; }

public class课程:BaseModel {
public Course(){}
[必需]
public int presentationId {get;组; }
public virtual Presentation presentation {get;组; }
}


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆