How do I use 2 include statements in a single MVC EF query?

Posted by alockrem on Stack Overflow See other posts from Stack Overflow or by alockrem
Published on 2012-06-28T01:41:03Z Indexed on 2012/06/28 3:16 UTC
Read the original article Hit count: 91

Filed under:
|
|

I am trying to write a query that includes 2 joins.

  • 1 StoryTemplate can have multiple Stories
  • 1 Story can have multiple StoryDrafts

I am starting the query on the StoryDrafts object because that is where it's linked to the UserId.

I don't have a reference from the StoryDrafts object directly to the StoryTemplates object. How would I build this query properly?

    public JsonResult Index(int userId)
    {
        return Json(
            db.StoryDrafts
                .Include("Story")
                .Include("StoryTemplate")
                .Where(d => d.UserId == userId)
            ,JsonRequestBehavior.AllowGet);
    }

Thank you for any help.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about entity-framework