Entity Framework: Get multiple linked Objects

.net-core c# entity-framework-core

Question

I did research on my problem, but somehow I don't get it.

I have 4 classes with linked entities in a 1:n way:

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; }
}

I like to get all data for a project, which I try to get this way by projectId from a repository:

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;
}

I am only able to go to the level of "UrlTextContent", but I am somehow not able to include "UrlTextSnippet".

My goal is to load a complete "Project" to be able to do some processing on the linked entities list items.

In the end I want to find all "UrlTextContent" for which no "UrlTextSnippets" are available by iteration over the linked entities.

I use .NET Core 2.1.403 with Entity Framework Core .NET 2.1.4-rtm-31024

Any help is very much appreciated.

Best regards

Edit:

Context class:

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; }
}

Edit 2: Debug screenshot

"UrlTextSnippet" list is null, although one entry is available.

enter image description here

1
1
10/28/2018 1:04:07 PM

Popular Answer

I hate those people that say you can do something you aren't able to do, so sorry in advance. According to the Documentation you should be able to chain those .ThenInclude() or the .Include(). Hopefully these get you on the right track.

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();
}

There is also this but I try to avoid selects if I can.

Additionally you might be running into this issue and you might be able to do it but the IntelliSense may be leading you astray.

Note: Current versions of Visual Studio offer incorrect code completion options and can cause correct expressions to be flagged with syntax errors when using the ThenInclude method after a collection navigation property. This is a symptom of an IntelliSense bug tracked at https://github.com/dotnet/roslyn/issues/8237. It is safe to ignore these spurious syntax errors as long as the code is correct and can be compiled successfully.

2
11/3/2018 5:04:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow