简体   繁体   English

查询EDMX中的多对多连接表

[英]Query to many-to-many joiner table in EDMX

I am currently facing performance problem whenever I'm accessing navigation property of an entity. 每当我访问实体的导航属性时,我目前都面临性能问题。

I have three tables: UserCategory , User and UserCategoryUser . 我有三个表: UserCategoryUserUserCategoryUser There is many-to-many relation between User and UserCategory tables. UserUserCategory表之间存在多对多关系。 UserCategoryUser table is the joiner table and it has two columns UserId and UserCategoryId ie the primary keys of User and UserCategory tables. UserCategoryUser表是joiner表,它有两列UserIdUserCategoryId,UserUserCategory表的主键。 The joiner table UserCategoryUser is used for maintaining the many-to-many relationship between User and UserCategory tables. 连接表UserCategoryUser用于维护UserUserCategory表之间的多对多关系。

I'm using database first approach. 我正在使用数据库第一种方法。 Hence in my EDMX, for the User entity I have one navigation property UserCategories . 因此,在我的EDMX中,对于User实体,我有一个导航属性UserCategories Similarly for the UserCategory entity there is a navigation property Users . 类似地,对于UserCategory实体,存在属性Users

I want to add a user to a usercategory. 我想将用户添加到用户类别。 So before adding, I'm doing a check whether the user is already added to the usercategory or not. 所以在添加之前,我正在检查用户是否已经添加到用户类别中。 In my database I've around 100k user records and only one usercategory. 在我的数据库中,我有大约10万个用户记录和一个用户类别。 All the users associated with the only usercategory. 与唯一usercategory关联的所有用户。

I'm accessing the Users navigation property like follows: 我正在访问Users导航属性,如下所示:

var userCategory = Context.UserCategories.FirstOrDefault(uc => uc.UserCategoryId == userCategoryId);

if (userCategory != null)
{
    if (userCategory.Users.Any(u => u.Username == username))
    {
        //Other operations
    }
}

This code was working before but now it is hanging as we have lots of user data. 这段代码以前工作过,但现在因为我们有很多用户数据而挂起。 In particular it is hanging for userCategory.Users.Any(u => u.Username == username)) line. 特别是它挂起userCategory.Users.Any(u => u.Username == username))行。 I also tried getting the users count but still it is hanging! 我也试过让用户count但它仍然悬挂!

userCategory.Users.Count();

I'm not able to do the join query in LINQ to Entity as the joiner table( UserCategoryUser ) is NOT added as an entity in the EDMX. 我无法在LINQ to Entity中执行连接查询,因为joiner表( UserCategoryUser )未作为EDMX中的实体添加。

How to solve this problem? 如何解决这个问题呢? I can to this check using pure SQL or stored procedure but I want to stay away from those for this case. 我可以使用纯SQL或存储过程进行此检查,但我想远离这种情况。

Your problem is that with lazy loading enabled, the call to userCategory.Users loads all Users into memory. 您的问题是,启用延迟加载后,对userCategory.Users的调用会将所有用户加载到内存中。 I think, given that there are very few categories, I would do it the other way round. 我认为,鉴于类别很少,我会反过来这样做。 ie get the User then check whether the user's Category collection contains the category being added. 即获取用户然后检查用户的类别集合是否包含要添加的类别。

However, if you had a very large number of categories then you could explicitly filter the collection. 但是,如果您有非常多的类别,那么您可以显式过滤该集合。 First turn off lazy loading on the collection (eg by removing the virtual keyword), then try this: 首先关闭集合上的延迟加载(例如,通过删除virtual关键字),然后尝试:

   Context.Entry(userCategory)
              .Collection(uc => uc.Users)
              .Query()
              .Where(u => u.Username == username)//I'd put an index on username too
              .Load();

   if(userCategory.Users.Any())
   {
      //Other operations
   }

Reference: https://msdn.microsoft.com/en-us/data/jj574232.aspx#explicitFilter 参考: https//msdn.microsoft.com/en-us/data/jj574232.aspx#explicitFilter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM