Request many, many relationships with Entity Framework and .NET 3.5

My problem is actually double. First off, I'm pretty new to EF, but I have used it with some success, using shortcuts before. Although, for this particular project, I plan to spend time studying.

What I have at the moment is a very simplified database structure:

Post =================== Id Title ------------------- 1 Hello world 2 Foo bar baz Tag =================== Id Title ------------------- 6 test 7 todo PostTags =================== PostId TagId ------------------- 1 6 1 7 2 7 

Using EF, he creates a model with two objects: Publish and Tag .

What I'm trying to accomplish:

I am trying to query the model for all messages defined by the tagIds array (e.g. 6 and 7). However, I cannot understand what I need to do to make this work. I used to cheat on the fact that I added auto-increment PK to the mapping table (BlogPosts) and added it to the EF model, but it was rather an ugly hack to move forward. I'd rather learn to do it now.

+4
source share
1 answer

This will work in EFv4. Try it in EFv1:

 var tagIds = new int[] { 6, 7 }; var query = context.Tags .Where(t => tagIds.Contains(t.Id)) .SelectMany(t => t.Posts); 

Edit:

I checked it, and evaluation parameters + Contains were added in EFv4, so the above example does not work in EFv1. This thread in the MSDN forum provides a workaround that allows the IN statement in EFv1. Another recommended solution is to simply upgrade to EFv4, because there are many other improvements, including a significant performance improvement.

+1
source

Source: https://habr.com/ru/post/1345373/


All Articles