实体框架,EntityCommandExecutionExeption

时间:2013-07-19 17:03:03

标签: asp.net-mvc asp.net-mvc-4 simplemembership

使用简单成员资格提供程序....和用户视图(将UserID传递到)Userroles Controller

我的索引(UserRoles)(ID是来自用户控制器视图中ActionLink的UserID)

    public ActionResult Index([Bind(Prefix = "id")] int id)
    {
        var usr = db.UserRoles.Find(id);

        if (usr != null)
        {
            return View(usr);
        }

        return View();
        //return View(db.UserRoles.ToList());
    }

但我的模特是

[Table("webpages_UsersInRoles")]
public class UserRoles
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public int RoleID { get; set; }

}

错误 enter image description here

由于

换句话说,它甚至不应该寻找UserProfiles_UserID .....?

我也是这样做的: 堆栈跟踪:

 [SqlException (0x80131904): Invalid column name 'UserProfiles_UserId'.
 Invalid column name 'UserProfiles_UserId'.

我的数据库或我的模型中也没有这个字段; /我很困惑为什么要查找它?

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码将用户添加到角色:

 System.Web.Security.Roles.AddUsersToRole("user1", "user2", "user3", ... , "yourrolename");