gpt4 book ai didi

c# - 在不使用 "Include"的情况下首先在 Entity Framework 5代码中获取子表记录

转载 作者:太空宇宙 更新时间:2023-11-03 11:04:01 25 4
gpt4 key购买 nike

我的应用程序我首先使用实体​​框架 5 代码我想获取子表数据而不使用像“包含”这样的

public class Category
{
public int Id {get; set;}
public string Name {get; set;}
public virtual ICollection<Product> Products{get;set;}
}

public class Product
{
public int id {get;set}
public string Name{get;set;}
public virtual Category {get;set;}
}

我需要双管齐下,所以有任何解决方案

最佳答案

我认为,在没有 Include 的情况下获取一个集合就像在 SQL 中获取一个没有 join 的集合:)

当然可以

var category = context.Categories.First(c=>c.Id == id);
category.Products = context.Products.Where(p=>p.CategoryId == id);

但是会导致两个db连接。 include 通常被翻译成 JOIN 语句,这是检索相关数据的常用解决方案。

在带有跟踪的 ObjectContext 的非 POCO 实体中,您可以使用 LoadWith 语句。但它实际上也包括在内

关于c# - 在不使用 "Include"的情况下首先在 Entity Framework 5代码中获取子表记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16648266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com