2

我一直在为我的应用程序使用实体框架。不幸的是,我不能在实体框架中做出这样的表达:

List<MyUser> users = (from user in database.MyUsers
                      join app in database.MyApplications 
                          on user.ApplicationId equals app.Id
                      where app.Name == this._applicationName 
                          && user.MyRoles.Contains(existingRole)
                      select user).ToList();

对此的任何其他方法。我不想更改我的数据库或模型。在我的例子中,MyUser 和 MyRole 之间的关系是多对多的,有一个胶水表。

这是 MyUser 类的声明方式:

public partial class MyUser
{
    public MyUser()
    {
        this.MyApiTokens = new HashSet<MyApiToken>();
        this.MyLandingPages = new HashSet<MyLandingPage>();
        this.MyPresentations = new HashSet<MyPresentation>();
        this.MySlides = new HashSet<MySlide>();
        this.MyUserSettings = new HashSet<MyUserSetting>();
        this.MyRoles = new HashSet<MyRole>();
    }

    public System.Guid Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string EmailAddress { get; set; }
    public int ApplicationId { get; set; }
    public System.DateTime CreateDate { get; set; }
    public System.DateTime LastActivityDate { get; set; }
    public System.DateTime LastLockoutDate { get; set; }
    public System.DateTime LastLoginDate { get; set; }
    public System.DateTime LastPasswordChangedDate { get; set; }
    public string PasswordQuestion { get; set; }
    public string PasswordAnswer { get; set; }
    public string Comment { get; set; }
    public bool IsLocked { get; set; }
    public bool IsApproved { get; set; }

    public virtual ICollection<MyApiToken> MyApiTokens { get; set; }
    public virtual MyApplication MyApplication { get; set; }
    public virtual ICollection<MyLandingPage> MyLandingPages { get; set; }
    public virtual ICollection<MyPresentation> MyPresentations { get; set; }
    public virtual ICollection<MySlide> MySlides { get; set; }
    public virtual ICollection<MyUserSetting> MyUserSettings { get; set; }
    public virtual ICollection<MyRole> MyRoles { get; set; }
}
4

1 回答 1

3

假设 MyRoles 是实体模型中的关联属性(即映射到表),您可能希望在角色对象的主键上进行显式匹配。例如:

user.MyRoles.Any(r => r.RoleId == existingRole.RoleId)

但是,您的问题中没有足够的信息来给出确切的答案。

于 2013-01-08T19:37:42.790 回答