gpt4 book ai didi

c# - 从 Entity Framework 中的集合加载相关实体

转载 作者:行者123 更新时间:2023-11-30 18:33:20 25 4
gpt4 key购买 nike

如何从已加载的集合中加载相关实体:

集合:

public class Ad
{
// Primary properties
[Key]
public int Id { get; set; }
private ICollection<Feature> _features;
public virtual ICollection<Feature> Features
{
get { return _features ?? (_features = new HashSet<Feature>()); }
set { _features = value; }
}
}

特点:

public class Feature
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }

// Navigation properties
public virtual ICollection<Ad> Ads { get; set; }
public Keyword Keyword { get; set; }
}

关键字:

public class Keyword
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}

我需要为广告中的所有功能加载实体关键字。

谢谢

最佳答案

在您的存储库类中尝试:

public Ad GetAd(int id)
{
return _database.Set<Ad>().Include(ad => ad.Features.Select(feature => feature.Keyword)).FirstOrDefault(ad => ad.Id == id);
}

关于c# - 从 Entity Framework 中的集合加载相关实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17625834/

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