我试图在EF中实现懒惰和急切的加载。 一切似乎都很好,但.Include()方法在两种情况下都可用,并返回相同的结果,无论延迟加载false还是true。
我有2个表类别和产品。 类别可以有多个产品和使用外键相关。
当我使用.Include("Products")使用延迟加载加载类别时,它会加载与类别相关的所有产品。
急切加载显示了相同的行为。
var results = objDB.Categories.Include("Products").ToList(); // Enabled lazy load var results = objDB.Categories.Include("Products").ToList(); // Disabled lazy load两条线上方都有相同的结果。 请澄清这种混乱。
如果延迟加载= true,则不能使用.Include 。
请提出宝贵的意见。 提前致谢。
I have tried to implement lazy and eager loading in EF. Everything seems fine, but the .Include() method is available in both cases and returns the same results irrespective of lazy loading false or true.
I have 2 tables categories and product. Category can have multiple products and related using foreign key.
When I load categories using lazy loading by using .Include("Products"), it loads all products related to categories.
Same behavior is shown by eager loading.
var results = objDB.Categories.Include("Products").ToList(); // Enabled lazy load var results = objDB.Categories.Include("Products").ToList(); // Disabled lazy loadAbove both lines have same result. Please clarify this confusion.
.Include should not be available in case of lazy loading = true.
Please give your valuable opinion. Thanks in advance.
最满意答案
当您明确使用Include() ,您正在执行Eager Loading。 显然,禁用/启用延迟加载不起作用。
当您省略Include并尝试访问某些Category实例中的Products navigation-property时,将反映出这种差异。 启用Lazy-Loading后,EF将从数据库加载它。 当它被禁用时,你将获得null 。
When you're explicitly using Include(), you're preforming an Eager Loading. Obviously, disabling/enabling Lazy Loading has no effect.
The difference will be reflected when you'll omit the Include and try to access the Products navigation-property in some of your Category instances. When Lazy-Loading is enabled, EF will load it from the database. When it's disabled, you'll get null.
更多推荐
发布评论