1

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

4
  • @Igotcha Plz provide the context class also
    – prisar
    Oct 28, 2018 at 11:21
  • You said "I am somehow not able to include "UrlTextSnippet". What does the result look like when you don't try to include it? Furthermore, are you getting an error or no returned items?
    – Aaron
    Oct 28, 2018 at 11:28
  • @Aaron The list of UrlTextSnippets is always "null". The expected behavior should be the number of entries.
    – Igotcha
    Oct 29, 2018 at 11:58
  • @t-prisar I added the context class and a screenshot.
    – Igotcha
    Oct 29, 2018 at 11:59

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.