Hi,
I went through the issues related to EFCore 2.1 and data seeding, but was not able to solve my issue.
Not sure if this is the expected behavior of using EFCore's .HasData() method but this is what I am currently experiencing.
I have the two basic classes created from the EFCore tutorial on Microsoft's site, namely Blog and Post.
Blog.cs
```C#
public int BlogId { get; set; }
public string Url { get; set; }
public List
Post.cs
```C#
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; }
AppDbContext.cs
```C#
public AppDbContext(DbContextOptions options) : base(options)
{
}
public DbSet
public DbSet
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity
(
new Blog
{
BlogId = 1,
Url = "www.sample.com"
}
);
modelBuilder.Entity<Post>().HasData
(
new {BlogId = 1, PostId = 1, Title = "First post", Content = "Test 1"},
new {BlogId = 1, PostId = 2, Title = "Second post", Content = "Test 2"}
);
}
``
Usingdotnet ef migrations add ...anddotnet ef database update` commands work without any errors and the database gets seeded.
However, when I select the blog to be displayed through the controller I simply get back an empty array of posts.

Adding the post data directly into the blog data sometimes throws an error and sometimes it does not, but the outcome is still the same as the image above.
In my Blog.cs file when I change my public List<Post> Posts { get; set; } = new List<Post>() to public List<Post> Posts { get; set; } it would just provide a null value instead on the empty array.
Am I missing something in my code to get this to work properly?
What's your API controller look like?
c#
return await _db.Blog
.Where(b => b.Id == 1)
.Include(b => b.Posts)
.SingleOrDefaultAsync()
I think that should work.
@kjbetz Thanks for the reply, but when I add your code to my controller it gives the following error,
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'blog' with type 'SeedingData.Models.Blog'. Path '[0].posts[0]'
I am able to retrieve a particular post without referencing

The code I am using in my controller is
return _context.Blogs.ToList();
Which worked prior to an update to EFCore 2.1 a few days ago.
Add the json options to your Startup.cs
c#
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
Here's a repro: https://github.com/kjbetz/BlogPosts
@kjbetz Thanks a lot, that worked :)
Sorry about that, new to GitHub and forgot to close the issue with the comment itself.
Most helpful comment
Add the json options to your
Startup.csc# services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; });Here's a repro: https://github.com/kjbetz/BlogPosts