如何在EF4中查询多对多映射? [英] How do I query many-to-many mapping in EF4?

查看:203
本文介绍了如何在EF4中查询多对多映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题,一个不太明显的解决方案。我在我的数据库中有一个关系映射在用户和角色之间,每个用户可以映射到一个或多个角色。所以映射如下:


用户< 1:n> UserRole< n:1>角色


在我生成的EF4 POCO中,用户和角色各有一个ICollection:

  public class User 
{
//束其他属性/方法

public virtual ICollection< ;作用>角色
}

public class Role
{
//束其他属性/方法

public virtual ICollection< User>用户
}

现在,我已经实现了IoC,UoW和存储库模式在这篇文章,它使用ObjectSet通过存储库获取/保存数据。



我的问题是,如何实现

  public bool UserIsInRole(int userId,int roleId)

我尝试了以下内容:

  public bool UserIsInRole(int userId,int roleId)
{
角色角色= _roleRepository.Single(r => r.Id == roleId);
return _userRepository.SingleOrDefault(u => u.Roles.Contains(role))!= null;
}

但是失败:


无法创建Data.Models.Role类型的常量值。在这种情况下,只支持原始类型(如Int32,String和Guid)。


另外,它不是一个



我正在寻找这样的东西:

  return _userRepository.SingleOrDefault(u => u.Roles.Where(r => r.Id = roleId)); 

但ICollection不支持LINQ。



如何做到这一点,理想情况下,如何使用一个LINQ表达式和一次访问数据库?



或者,我是否完全出错?



提前感谢



已解决:
感谢所有发贴的人。都给了可以接受的答案。我接受了最优雅的一个。

解决方案

有一个更简洁的方法:

  public bool UserIsInRole(int userId,int roleId)
{
return _userRepository。
Any(u => u.Id == userId&&
u.Roles.Any(r => r.Id == roleId));
}


I have a pretty simple problem with a not-so-obvious solution. I have a relational mapping in my database between Users and Roles, and each user can be mapped to one or more roles. So the mapping is like so:

User < 1:n > UserRole < n:1 > Role

In my generated EF4 POCOs, User and Role each have an ICollection of the other:

public class User 
{
    //Bunch of other properties/methods

    public virtual ICollection<Role> Roles
}

public class Role
{
    //Bunch of other properties/methods

    public virtual ICollection<User> Users
}

Now, I've implemented the IoC, UoW, and repository patterns illustrated in this article, which uses an ObjectSet to fetch/persist the data via repositories.

My question is, how do I implement this:

public bool UserIsInRole(int userId, int roleId)

I have tried the following:

public bool UserIsInRole(int userId, int roleId)
{
    Role role = _roleRepository.Single(r => r.Id == roleId);
    return _userRepository.SingleOrDefault(u => u.Roles.Contains(role)) != null;
}

But it fails with:

Unable to create a constant value of type 'Data.Models.Role'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

Plus, it's not a very elegant implementation as it's having to hit the database twice.

I was looking for something like this:

return _userRepository.SingleOrDefault(u => u.Roles.Where(r => r.Id = roleId));

But ICollection doesn't support LINQ.

How can I do this, and ideally, how can I do it with one LINQ expression and one trip to the database?

Or, am I going about this completely wrong?

Thanks in advance.

Solved: Thanks to all who posted. All gave an acceptable answer. I accepted the one that was the most elegant.

解决方案

There is a more concise way to do it:

public bool UserIsInRole(int userId, int roleId)
{
    return _userRepository.
            Any(u => u.Id == userId && 
                     u.Roles.Any(r => r.Id == roleId));
}

这篇关于如何在EF4中查询多对多映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆