【问题标题】:Entity Framework: Get multiple linked Objects实体框架:获取多个链接对象
【发布时间】:2018-10-28 11:04:31
【问题描述】:

我对我的问题进行了研究,但不知何故我不明白。

我有 4 个类以 1:n 的方式链接实体:

public class ContentProtectionProject
{
    [Required]
    public int Id { get; set; }

    ...

    [Required]
    public List<UrlToProtect> UrlsToProtect { get; set; }

    [Required]
    public int AccountId { get; set; }

    [ForeignKey("AccountId")]
    public Account Account { get; set; }
}

public class UrlToProtect
{
    [Required]
    public int Id { get; set; }

    ...

    [Required]
    public List<UrlTextContent> UrlsTextContent { get; set; }

    [Required]
    public int ContentProtectionProjectId { get; set; }

    [ForeignKey("ContentProtectionProjectId")]
    public ContentProtectionProject ContentProtectionProject { get; set; }
}

public class UrlTextContent
{
    [Required]
    public int Id { get; set; }

    ...

    [Required]
    public List<UrlTextSnippet> UrlTextSnippets { get; set; }

    [Required]
    public int UrlToProtectId { get; set; }

    [ForeignKey("UrlToProtectId")]
    public UrlToProtect UrlToProtect { get; set; }
}

public class UrlTextSnippet
{
    [Required]
    public int Id { get; set; }

    ...

    [Required]
    public int UrlTextContentId { get; set; }

    [ForeignKey("UrlTextContentId")]
    public UrlTextContent UrlTextContent { get; set; }
}

我喜欢获取项目的所有数据,我尝试通过 projectId 从存储库中获取这些数据:

public async Task<ContentProtectionProject> GetContentProtectionProject(int contentprotectionProjectId)
{
    var contentProtectionProject = await _context.ContentProtectionProjects
        .Include(x => x.UrlsToProtect)
        .ThenInclude(u => u.UrlsTextContent)

        .FirstOrDefaultAsync(x => x.Id == contentprotectionProjectId);

    return contentProtectionProject;
}

我只能进入“UrlTextContent”级别,但不知何故无法包含“UrlTextSnippet”。

我的目标是加载一个完整的“项目”,以便能够对链接实体列表项进行一些处理。

最后,我想通过对链接实体的迭代找到所有没有“UrlTextSnippets”可用的“UrlTextContent”。

我将 .NET Core 2.1.403 与 Entity Framework Core .NET 2.1.4-rtm-31024 一起使用

非常感谢任何帮助。

最好的问候

编辑:

上下文类:

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options) : base (options) {}
    ...
    public DbSet<ContentProtectionProject> ContentProtectionProjects { get; set; }
    public DbSet<UrlToProtect> UrlToProtects { get; set; }
    public DbSet<UrlTextContent> UrlTextContents { get; set; }
    public DbSet<UrlTextSnippet> UrlTextSnippets { get; set; }
}

编辑 2:调试屏幕截图

“UrlTextSnippet”列表为空,尽管有一个条目可用。

【问题讨论】:

  • @Igotcha Plz 也提供上下文类
  • 您说“我不知何故无法包含“UrlTextSnippet”。如果您不尝试包含它,结果会怎样?此外,您是否收到错误或没有返回项目?
  • @Aaron UrlTextSnippets 列表始终为“null”。预期的行为应该是条目数。
  • @t-prisar 我添加了上下文类和屏幕截图。

标签: c# .net-core entity-framework-core


【解决方案1】:

我讨厌那些说你可以做你不能做的事情的人,所以提前抱歉。根据Documentation,您应该能够链接那些.ThenInclude().Include()。希望这些能让你走上正轨。

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
    .Include(blog => blog.Posts)
        .ThenInclude(post => post.Author)
        .ThenInclude(author => author.Photo)
    .Include(blog => blog.Owner)
        .ThenInclude(owner => owner.Photo)
    .ToList();
}

还有this,但我尽量避免选择。

此外,您可能会遇到此问题并且您可能能够做到,但 IntelliSense 可能会导致您误入歧途。

注意:当前版本的 Visual Studio 提供不正确的代码完成 选项,并可能导致用语法标记正确的表达式 在集合导航后使用 ThenInclude 方法时出错 财产。这是跟踪到的 IntelliSense 错误的症状 https://github.com/dotnet/roslyn/issues/8237。忽略是安全的 这些虚假的语法错误只要代码正确并且可以 编译成功。

【讨论】:

    【解决方案2】:

    坚持我的“不知何故”。昨天我沮丧地关闭了我的 Visual Studio,今天我想再次测试它,现有的代码和那个没有任何更改。

    也许我的 Visual Studio Code 昨天出了点问题。

    不过还是感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多