Troubles with Entity Framework Get Started

Semen Veslov 1 Reputation point
2022-01-22T21:57:42.897+00:00

I tried play with ef(Net6.0, ef 6.0.1, Sqlite) I took code from "Getting Started with EF Core". I have adds some modifications to Program.cs:

var allblogs = db.Blogs
.OrderBy(b => b.BlogId) ;
foreach(Blog b in allblogs)
{
Console.WriteLine($"]]] Blog: {b.BlogId}, url: {b.Url}") ;
var posts = b.Posts ;
foreach(Post p in posts)
{
Console.WriteLine($"---> Post: {p.PostId}, title: {p.Title}, content: {p.Content}") ;
}
}

i expected get something like it:

]]] Blog: ith, ...
---> Post: jth, ...
...
]]] Blog: (i+1)th,...
-> Post
...

`
But instead, i got:

]]] Blog: 3
]]] Blog: 4
...
Post: 84, title: Hello World 0 : 23.01.2022 00:20:18, content: I wrote an app using EF Core! 0-- 23.01.2022 00:20:18 !!!!!!
Post: 85, title: Hello World 1 : 23.01.2022 00:20:19, content: I wrote an app using EF Core! 1-- 23.01.2022 00:20:19 !!!!!!
Post: 86, title: Hello World 2 : 23.01.2022 00:20:19, content: I wrote an app using EF Core! 2-- 23.01.2022 00:20:19 !!!!!!
....

Why? Can somebody explain to me what happens here?

Developer technologies | .NET | Entity Framework Core
Developer technologies | .NET | .NET Runtime
Developer technologies | .NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-01-24T20:25:19.98+00:00

    Given the following

    167995-bp.png

    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.]  
      
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.