Complex queries with Entity Framework RRS feed

  • Question

  • User69225619 posted

    I need to write a small blog (just a task at university). It involves a page that shows a single blog post and all comments for it. With PHP and SQL it would be a no-brainer, but I don't quite understand how would that work with Entity Framework. Should I make it so each blog post has a field that is a list of IDs of related comments, or should I make every ID have a field that points to a related blog post?

    Friday, March 17, 2017 9:04 AM

All replies

  • User753101303 posted


    Using EF shouldn't change anything to your database design. Usually you just don't have multivalued fields. Instead each "child" element is related to its parent using the ID for the parent. So here more likely you would have each comment having the ID for the blog post it belongs to (not sure if you want to track comments answered to previous comments ?)

    On the EF side it will be seen as a collection of related comments for each blog post.

    Friday, March 17, 2017 9:17 AM
  • User69225619 posted

    Ehm, is there a way to make Entity Framework code generation more predictable? If I do my model the 'code-first' way, it does not necessarily create what I want. If I try the 'db-first' approach and make EF generate model classes - it is even worse.

    Friday, March 17, 2017 12:30 PM
  • User753101303 posted

    Try perhaps to start from https://msdn.microsoft.com/en-us/library/jj591583(v=vs.113).aspx which uses blogs, posts and comments. It should generate the same database for both of us and then we would need to know how it differs from what you expect.

    Friday, March 17, 2017 12:41 PM