Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
(Full code available for cloning here) I've run into some odd behavior of EF-core's Find method. It seems like the returned entity doesn't include the rest of the data. MWE Models using Micro...
#1: Initial revision
EF-core Find method doesn't include other entities
(Full code available for cloning [here](https://github.com/MoshiKoi/EF-Core-Find-Behaviour)) I've run into some odd behavior of EF-core's [`Find` method](https://learn.microsoft.com/en-us/ef/core/change-tracking/entity-entries#find-and-findasync). It seems like the returned entity doesn't include the rest of the data. ## MWE <details> <summary>Models</summary> ```cs using Microsoft.EntityFrameworkCore; public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } public string DbPath { get; } public BloggingContext() { var folder = Environment.SpecialFolder.LocalApplicationData; var path = Environment.GetFolderPath(folder); DbPath = System.IO.Path.Join(path, "blogging.db"); } // The following configures EF to create a Sqlite database file in the // special "local" folder for your platform. protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite($"Data Source={DbPath}"); } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List<Post> Posts { get; } = new(); } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } ``` </details> ```cs int id; Console.WriteLine("Setting up database:"); using (var db = new BloggingContext()) { Console.WriteLine($" - Database path: {db.DbPath}."); Console.WriteLine(" - Adding blog: http://blogs.msdn.com/adonet"); var blog = new Blog { Url = "http://blogs.msdn.com/adonet" }; db.Add(blog); db.SaveChanges(); id = blog.BlogId; } Console.WriteLine($"Blog Id: {id}"); Console.WriteLine("Adding post"); using (var db = new BloggingContext()) { var blog = db.Blogs.Find(id); blog.Posts.Add(new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" }); db.SaveChanges(); Console.WriteLine($" - Number of Posts: {blog.Posts.Count()}"); } Console.WriteLine($"Test: Finding blog with id: {id}"); using (var db = new BloggingContext()) { var blog = db.Blogs.Find(id); Console.WriteLine($" - Found blog: {blog.Url}"); Console.WriteLine($" - Number of Posts: {blog.Posts.Count()}"); } Console.WriteLine("Delete the blog"); using (var db = new BloggingContext()) { var blog = db.Blogs.Find(id); db.Remove(blog); db.SaveChanges(); } ``` ## Result ``` Setting up database: - Database path: C:\Users\[redacted]\AppData\Local\blogging.db. - Adding blog: http://blogs.msdn.com/adonet Blog Id: 15 Adding post - Number of Posts: 1 Test: Finding blog with id: 15 - Found blog: http://blogs.msdn.com/adonet - Number of Posts: 0 Delete the blog ``` Note: The 15 is just because I've run this a couple of times and it auto-incremented. As you can see, it says that there are 0 posts in the blog returned by the `Find` method, even though I added one before. I set a breakpoint before the blog deletion cleanup code and checked with a database viewer and the `Post` is indeed stored into the database with the correct `Blog` id. I'm probably missing something obvious, but why doesn't it return the posts that I added before? And how do I get it to do so? Is there some configuration somewhere that manages this?