【发布时间】: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