Given the following
I don't have .NET 6/EF Core 6, only .NET 5/EF Core 5 which should still work for you.
Replace Debug.WriteLine with Console.WriteLine
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BlogPostsFormsProject.Data;
using Microsoft.EntityFrameworkCore;
namespace BlogPostsFormsProject.Classes
{
public class Operations
{
public static async Task Example1()
{
Debug.WriteLine(nameof(Example1));
await using var context = new Context();
var blogs = await context
.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.PostTag)
.ThenInclude(postTags => postTags.Tags)
.FirstOrDefaultAsync();
foreach (var post in blogs.Posts)
{
if (post.PostTag.Count <= 0) continue;
Debug.WriteLine(post.Title);
Debug.WriteLine($"\t{post.Content}");
foreach (var tag in post.PostTag)
{
Debug.WriteLine($"\t\t{tag.TagsId}");
}
}
Debug.WriteLine("");
}
public static async Task Example2()
{
Debug.WriteLine(nameof(Example2));
await using var context = new Context();
var blogs = await context
.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.PostTag)
.ThenInclude(postTags => postTags.Tags)
.ToListAsync();
foreach (var blog in blogs)
{
Debug.WriteLine($"Name: {blog.Name,-20}Url:{blog.Url}");
foreach (var blogPost in blog.Posts)
{
Debug.WriteLine($"Post title: {blogPost.Title}");
Debug.WriteLine($"Content: [{blogPost.Content}]");
}
Debug.WriteLine("");
}
Debug.WriteLine("");
}
}
}
usage
await Task.Run(async () => await Operations.Example1());
.
.
.
await Task.Run(async () => await Operations.Example2());
Added DateModified
Debug.WriteLine($"Name: {blog.Name,-20}Url:{blog.Url} {blog.DateModified:d}");
Sample output
Example2
The thread 0x2834 has exited with code 0 (0x0).
Name: Developer insights Url:https://paynekaren.blogspot.com/ 11/19/2020
Post title: .NET Core TaskDialog
Content: [Some text about TaskDialog]
Post title: GitHub download partial repository
Content: [There may be times when a developer wants one or two projects from a GitHub repository rather than downloading the entire repository.]
Post title: Entity Framework Core Find method code samples
Content: [At one time or another a record needs to be located, usually developers will use FirstOrDefault or SingleOrDefault (along with First and Single) to find a single records.
When working with a primary key using the Find and FindAsync method will find entities in the added state that are not yet persisted while the Where method will not and need to query the database. Also, in many cases Find will be a good deal faster than Where.
What developers need to do is learn what methods are available and work with the appropriate method for a specific task e.g. a model has City name, using Find will not work as it's used on primary keys while FirstOrDefault to find the first city interested in or Where to find all cities interested in.
Here the Customers table has a single primary key, FindAsync will if a entity is located with the key passed in keys will record an entity while navigation properties will not be included.]
Name: Second blog Url:www.myotherblog 11/19/2020
Post title: Fantastic Cheese in Germany
Content: [TODO]
Post title: Entity Framework/Entity Framework Core connection strings for Windows Forms
Content: [When creating a DbContext for Entity Framework 6 or Entity Framework Core by default the constructor for Entity Framework 6 has the name for the connection string and for Entity Framework Core is embedded in OnConfigurating.]