开发者

Many to Many Query in Entity Framework 4

开发者 https://www.devze.com 2023-02-22 17:13 出处:网络
I have have a many to many relationship in my database. The two end tables are B开发者_StackOverflow中文版logPost and Item and the table in the middle is ItemBlogPost.I need to get back all of the Blo

I have have a many to many relationship in my database. The two end tables are B开发者_StackOverflow中文版logPost and Item and the table in the middle is ItemBlogPost. I need to get back all of the BlogPosts related to a specific item. In SQL I would do it like this:

SELECT BlogPost.*
FROM BlogPost
    JOIN ItemBlogPost ON BlogPost.ID = ItemBlogPost.BlogPost_ID
WHERE ItemBlogPost.Item_ID = @Item_ID

In C# I have something similar:

IQueryable<BlogPost> itemBlogPosts = from b in connection.BlogPosts
                                     where b.Items == item.ID 
                                     orderby b.Content.CreateDate descending
                                     select b;

However, the line marked b.Items doesn't give me a list of the Item properties and there is no b.ItemBlogPost to look at the intermediary table. I also tried doing b.Items.Contains(item) but that also failed. How can I make this work in LINQ to EF4?


What about this:

var itemBlogPosts = from i in connection.Items
                    from b in i.BlogPosts // I suppose you have a nav. property on Item
                    where i.Id == itemId
                    select b; 

The same query can be also defined by:

var itemBlogPosts = connection.Items
                              .Where(i => i.Id == itemId)
                              .SelectMany(i => i.BlogPosts);


Can you just do this:

var itemBlogPosts = connection.Items.Single(b => b.ID == item.ID).BlogPosts;

Since you are using EF it should handle the many-to-many mapping for you and you should have BlogPosts as a navigation item in your Item Object.


If:

  1. You generated a model from the database (so not model-first)
  2. The connecting table contains exactly two foreign keys (without additional columns)
  3. The foreign keys were set up in the right way

Then:

  1. EF would have generated classes for you that contain so-called navigation properties on "both sides". A navigation property (collection of Items) on BlogPost and a nav.prop. (collection of BlogPosts) on Item.

This way:

  1. You can traverse the object graph bidirectional. Getting all the blogposts for a specific item or the other way around getting all the items for a certain blogpost.

So when you have your specific item by hand you can just create a collection of related blogposts by doing:

Item item = context.Items.Include("BlogPosts").FirstOrDefault<Item>(i => i.ID = someID)

Now you have a specific item with the collection of blogposts filled (if any were related to this item).

0

精彩评论

暂无评论...
验证码 换一张
取 消