Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据库中的ASP.NETMVC模型_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4_Model - Fatal编程技术网

C# 数据库中的ASP.NETMVC模型

C# 数据库中的ASP.NETMVC模型,c#,asp.net,asp.net-mvc,asp.net-mvc-4,model,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Model,我从DB创建了我的模型,其中有表Users、Roles和UsersInRoles,这是前两个表之间的中间表。 我的问题是,既然没有将表UsersInRoles添加到模型中,我如何将Roles=something的特定用户作为对象 public partial class Role { public Role() { this.Users = new HashSet<User>(); }

我从DB创建了我的模型,其中有表Users、Roles和UsersInRoles,这是前两个表之间的中间表。 我的问题是,既然没有将表UsersInRoles添加到模型中,我如何将Roles=something的特定用户作为对象

public partial class Role
    {
        public Role()
        {
            this.Users = new HashSet<User>();
        }

        public int RoleID { get; set; }
        public string RoleName { get; set; }
        public string RoleDescription { get; set; }

        public virtual ICollection<User> Users { get; set; }
    }

public partial class User
    {
        public User()
        {
            this.Roles = new HashSet<Role>();
        }

        public int UserID { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual ICollection<Role> Roles { get; set; }
    }

public ActionResult Index()
        {
            var users = db.Users;
            return View(users.ToList());
        }

诚然,我对hashset没有太多的经验,但是有没有理由一个简单的Linq查询不起作用呢

var requestedUsers = from UsersInRoles 
                     in db.Users 
                     where UsersInRoles.Roles.Contains(DesiredRoleHere) 
                     select UsersInRoles;

我相信这会奏效的

        var usersInRole = db.Users.Where(x => x.Roles.Any(y => y.RoleId == roleid));