1

In the following Model example (taken from this official ASP.NET site) I understand they are defining a Foreign Key relationship between Blogs (parent) and Posts (Child) tables. But in the Blog class below what is the use of public List<Post> Posts { get; set; } property? If I remove this property and generate SQL Server database out of this Model, I can still see database was successfully created with BlogId in Posts table as Foreign Key to Blog table

namespace EFGetStarted.AspNetCore.NewDb.Models
{
    public class BloggingContext : DbContext
    {
        public BloggingContext(DbContextOptions<BloggingContext> options)
            : base(options)
        { }

        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; set; }
    }

    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; }
    }
}
3
  • 1
    List<Post> Posts is a navigation property which makes it convenient to access Posts from a Blog. Commented Nov 7, 2016 at 17:26
  • 1
    @Jasen your link is broken, it should be msdn.microsoft.com/en-us/library/jj713564(v=vs.113).aspx Commented Nov 7, 2016 at 17:34
  • Seems I copied in an additional letter at the end of that URL. Commented Nov 7, 2016 at 17:39

1 Answer 1

3

Nam,

The Posts property in the Blog class could be useful if you have the need to access the posts directly from an instance of a Blog object.

Loading the posts could be done via lazy loading or Entity Framework Eager loading using the Include method:

    var blogs1 = context.Blogs 
                      .Include(b => b.Posts) 
                      .ToList(); 

If you remove it will not prevent the database to be properly created.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually EfCore doesn't support lazy loading yet, you have to use Include. And just to clarify the FK is created based on BlogId, the Posts property is merely a navigation property.
@VivienChevallier So your query is saying give me all posts of which blog? Or, your query is saying give me each blog along with all the posts associated with each blog. In other words, it's another way of writing a LINQ JOIN between Blogs and Posts tables. I'm trying to understand your response before marking it as an Answer.
@nam My query is saying: give me each blog along with all the posts associated with each blog.
@VivienChevallier And this query would have not worked without the property public List<Post> Posts { get; set; } used in the Blog Class?
Exactly, you would not have been able to write it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.