C# 实体框架:获取多个链接对象
我对我的问题做了研究,但不知何故我不明白 我有4个类,以1:n的方式链接实体:C# 实体框架:获取多个链接对象,c#,.net-core,entity-framework-core,C#,.net Core,Entity Framework Core,我对我的问题做了研究,但不知何故我不明白 我有4个类,以1:n的方式链接实体: public class ContentProtectionProject { [Required] public int Id { get; set; } ... [Required] public List<UrlToProtect> UrlsToProtect { get; set; } [Required] public int Acc
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; }
}
公共类ContentProtectionProject
{
[必需]
公共int Id{get;set;}
...
[必需]
公共列表UrlsToProtect{get;set;}
[必需]
public int AccountId{get;set;}
[外汇钥匙(“账户ID”)]
公共帐户{get;set;}
}
公共类UrlToProtect
{
[必需]
公共int Id{get;set;}
...
[必需]
公共列表UrlsTextContent{get;set;}
[必需]
public int ContentProtectionProjectId{get;set;}
[ForeignKey(“ContentProtectionProjectId”)]
公共ContentProtectionProject ContentProtectionProject{get;set;}
}
公共类UrlTextContent
{
[必需]
公共int Id{get;set;}
...
[必需]
公共列表UrlTextSnippets{get;set;}
[必需]
public int UrlToProtectId{get;set;}
[外键(“UrlToProtectId”)]
公共UrlToProtect UrlToProtect{get;set;}
}
公共类UrlTextSnippet
{
[必需]
公共int Id{get;set;}
...
[必需]
public int UrlTextContentId{get;set;}
[ForeignKey(“UrlTextContentId”)]
公共URL文本内容URL文本内容{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;
}
公共异步任务GetContentProtectionProject(int-contentprotectionProjectId)
{
var contentProtectionProject=wait_context.ContentProtectionProjects
.Include(x=>x.UrlsToProtect)
.然后包括(u=>u.UrlsTextContent)
.FirstOrDefaultAsync(x=>x.Id==contentprotectionProjectId);
返回contentProtectionProject;
}
我只能进入“UrlTextContent”级别,但不知何故,我无法包含“UrlTextSnippet”
我的目标是加载一个完整的“项目”,以便能够对链接实体列表项进行一些处理
最后,我希望通过对链接实体的迭代找到所有“UrlTextContent”,其中没有“UrlTextSnippets”
我将.NET Core 2.1.403与实体框架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; }
}
公共类DataContext:DbContext
{
公共DataContext(DbContextOptions选项):基本(选项){}
...
公共DbSet ContentProtectionProjects{get;set;}
公共数据库集UrlToProtects{get;set;}
公共数据库集UrlTextContents{get;set;}
公共数据库集UrlTextSnippets{get;set;}
}
编辑2:调试屏幕截图
“UrlTextSnippet”列表为空,但有一个条目可用
坚持我的“不知何故”。昨天我沮丧地关闭了我的Visual Studio,今天我想再次测试它,没有任何更改,现有的代码就这样消失了
也许我的Visual Studio代码昨天出了问题
但无论如何还是要感谢你的帮助。我讨厌那些说你可以做一些你做不到的事情的人,所以提前道歉。根据你应该能够链这些或。希望这些能让你走上正轨
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();
}
也有,但我尽量避免选择,如果我可以
此外,您可能会遇到这个问题,您可能能够做到这一点,但智能感知可能会将您引入歧途
注意:Visual Studio的当前版本提供了错误的代码完成
选项和可导致使用语法标记正确的表达式
在集合导航后使用ThenInclude方法时出错
财产。这是跟踪到的IntelliSense错误的症状
. 忽视是安全的
只要代码是正确的,这些虚假的语法错误就可以纠正
编译成功
@Igotcha Plz还提供了上下文类,您说“我不知怎么地无法包含”UrlTextSnippet“。当您不尝试包含它时,结果会是什么样子?此外,您是否收到错误或没有返回项?@Aaron UrlTextSnippet列表始终为“null”“。预期的行为应该是条目数。@t-prisar我添加了上下文类和屏幕截图。