Home > Net >  What is the benefit of having the back reference property in the foreign key table?
What is the benefit of having the back reference property in the foreign key table?

Time:01-09

In class Post, what is the benefit of specifying the optional property public Blog Blog { get; set; }?

public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
        public int Rating { 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; }
    }

CodePudding user response:

the main benefit is that you can use Include instead of explicitly joining tables

var posts=context.Posts.Include(i=>i.Blog).ToList();

CodePudding user response:

If you have an object of the class Post, it knows which blog it belongs to. If this property is not set, you would need to search all the posts in all the blogs, if you want to find that out.

  •  Tags:  
  • Related