IE可在延迟加载EF中使用

编程入门 行业动态 更新时间:2024-10-26 19:35:03
本文介绍了IE可在延迟加载EF中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在EF Code First中有两个模型:

I have two models in EF Code First:

public class Book { public int Id { get; set; } public virtual IEnumerable<Page> Pages { get; set; } } public class Page { public int Id { get; set; } public int BookId { get; set; } public Book Book { get; set; } }

从数据库加载Book模型时,Pages属性为Null。但是,当替换IEnumerable => ICollection时,延迟加载将起作用,并且页面将从数据库填充。 IEnumerable和延迟加载如何一起使用?

When loads a Book model from DB, Pages property is Null. But when replace IEnumerable => ICollection, lazy loading works and Pages fills from DB. How use IEnumerable and lazy loading together?

推荐答案

IEnumerable 是不可变的集合您无法修改(添加或删除)。 EF不支持此类型,因为EF内部需要修改模型中的集合。

IEnumerable is immutable collection which you cannot modify (add or remove). EF does not support this type because internally EF need to modify collection in model.

使用 ICollection 代替, ICollection 继承自 IEnumerable ,因此它不仅仍然具有延迟执行(延迟加载)的目的,还拥有更多修改集合的方法。

Use ICollection instead, ICollection inherits from IEnumerable so it not only still get deferred execution (lazy loading) purpose but also has more methods to modify collection.

更多推荐

IE可在延迟加载EF中使用

本文发布于:2023-11-14 10:37:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586987.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可在   加载   EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!