ASP.NET - 具有字符串ID和2个键的查询表

时间:2016-08-22 08:01:59

标签: c# mysql asp.net-mvc foreign-keys key

我目前停留在一段返回异常的代码上:

控制器:

 public ActionResult Edit (string userId)
    {
        AspNetUserRoles personRole;
        if (userId == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        ///error happens here during execution of  Find() on table
        personRole = tbls.AspNetUserRoles.Find(userId);


        if (personRole == null)
        {
            return HttpNotFound();
        }
        return View(personRole);
    }

VIEW(通过它传递字符串ID):

@model IEnumerable<WebApplication1.Models.MyViewModel>



<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>

        <th>
            @Html.DisplayNameFor(model => model.Id)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.RoleId)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.Id)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RoleId)
        </td>
        <td>
            @*I added null, so for the sake of routing*@
            @Html.ActionLink("Edit", "Edit", "Home", new { id = item.Id }, null) |



            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
    </tr>
}

</table>

模型:

  public class MyViewModel
    {

        public string Id { get; set; }
        public string Name { get; set; }
        public string RoleId { get; set; }
    }
 public partial class AspNetUserRoles
    {
        [Key]
        [Column(Order = 0)]
        public string UserId { get; set; }

        [Key]
        [Column(Order = 1)]
        public string RoleId { get; set; }

        public virtual AspNetUsers AspNetUsers { get; set; }
    }
}

public partial class AspNetUsers
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public AspNetUsers()
        {
            AspNetUserRoles = new HashSet<AspNetUserRoles>();
        }

        public string Id { get; set; }

        [StringLength(256)]
        public string Email { get; set; }

        public bool EmailConfirmed { get; set; }

        public string PasswordHash { get; set; }

        public string SecurityStamp { get; set; }

        public string PhoneNumber { get; set; }

        public bool PhoneNumberConfirmed { get; set; }

        public bool TwoFactorEnabled { get; set; }

        public DateTime? LockoutEndDateUtc { get; set; }

        public bool LockoutEnabled { get; set; }

        public int AccessFailedCount { get; set; }

        [Required]
        [StringLength(256)]
        public string UserName { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<AspNetUserRoles> AspNetUserRoles { get; set; }
    }
}

我想知道这里可能出现什么问题。我可以在字符串ID上循环连接AspNetUsers和AspNetUserRoles,它会提供所有记录(根据MyViewModel键入)。然后在视图中我希望能够获得针对AspNetUserRoles键入的特定记录并能够编辑它,尽管它现在无关紧要,因为我在查询数据库时无法获得正确的记录。

我的想法是使用@ Html.ActionLink(“编辑”,“编辑”,“主页”,新的{id = item.Id},null)进行路由问题(检查URI是什么样的,它是正确的意思控制器/动作/参数是好的,也是在debbuging字符串Id不是null时)或者说Find()方法的问题,虽然它需要字符串参数或我查询具有2个键的表的事实(尽管它只是一个想法没有什么可靠的。)

任何潜在客户都可能有用。

谢谢!

1 个答案:

答案 0 :(得分:1)

Find方法使用主键查找实体。由于您在AspNetUserRoles模型上有复合主键,因此必须提供两个键来查找实体:

personRole = tbls.AspNetUserRoles.Find(userId, roleId);

roleId是您要查找的角色的ID。如果您只有一个与用户关联的角色,则可以查询并将其设置为:

personRole = tbls.AspNetUserRoles.Single(m => m.UserId == userId);

但请记住,如果用户上面有多个角色代码,则会抛出异常。

详细了解如何查找实体here