C# ASP.NET-查询';使用字符串ID和2个键创建表
我目前正在处理一段返回异常的代码: 控制器:C# ASP.NET-查询';使用字符串ID和2个键创建表,c#,mysql,asp.net-mvc,foreign-keys,key,C#,Mysql,Asp.net Mvc,Foreign Keys,Key,我目前正在处理一段返回异常的代码: 控制器: public ActionResult Edit (string userId) { AspNetUserRoles personRole; if (userId == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ///error happe
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);
}
视图(我通过它传递字符串ID):
@model IEnumerable
@ActionLink(“新建”、“创建”)
@DisplayNameFor(model=>model.Id)
@DisplayNameFor(model=>model.Name)
@DisplayNameFor(model=>model.RoleId)
@foreach(模型中的var项目){
@DisplayFor(modeleItem=>item.Id)
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modeleItem=>item.RoleId)
@*我添加了null,所以为了路由*@
@ActionLink(“编辑”、“编辑”、“主页”,新的{id=item.id},null)|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
型号:
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; }
}
}
公共类MyViewModel
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共字符串RoleId{get;set;}
}
公共部分类AspNetUserRoles
{
[关键]
[第列(顺序=0)]
公共字符串用户标识{get;set;}
[关键]
[第列(顺序=1)]
公共字符串RoleId{get;set;}
公共虚拟AspNetUsers AspNetUsers{get;set;}
}
}
公共部分类AspNetUsers
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共AspNetUsers()
{
AspNetUserRoles=newhashset();
}
公共字符串Id{get;set;}
[StringLength(256)]
公共字符串电子邮件{get;set;}
公共布尔值{get;set;}
公共字符串密码哈希{get;set;}
公共字符串SecurityStamp{get;set;}
公共字符串PhoneNumber{get;set;}
public bool phonenumberconfirm{get;set;}
公共bool TwoFactorEnabled{get;set;}
公共日期时间?LockoutEndDateUtc{get;set;}
public bool LockoutEnabled{get;set;}
public int AccessFailedCount{get;set;}
[必需]
[StringLength(256)]
公共字符串用户名{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection AspNetUserRoles{get;set;}
}
}
我想知道这里可能出了什么问题。我可以在字符串ID上循环加入的AspNetUsers和AspNetUserRoles,并给出所有记录(根据MyViewModel键入)。然后在视图中,我希望能够获得针对AspNetUserRoles键入的特定记录,并能够对其进行编辑,尽管现在这与此无关,因为我在查询数据库时无法获得正确的记录
我的想法是,要么是使用@Html.ActionLink(“Edit”、“Edit”、“Home”、new{id=item.id},null”)进行路由的问题(检查了URI的外观,它的正确含义是controller/action/parameter是ok的,也就是在对字符串id不为null进行除错时),要么是Find()的问题方法,而它接受字符串参数或我正在查询具有2个键的表的事实(尽管这只是一个想法,但并不是很可靠)
任何线索都是有用的
谢谢
Find
方法使用主键查找实体。由于您在AspNetUserRoles
模型上有复合主键,因此必须提供两个键才能查找实体:
personRole = tbls.AspNetUserRoles.Find(userId, roleId);
roleId
是您要查找的角色的id。如果您只有一个与用户关联的角色,则可以通过以下方式查询并获取该角色:
personRole = tbls.AspNetUserRoles.Single(m => m.UserId == userId);
但请记住,若用户有多个角色,上述代码将抛出异常
阅读有关查找实体的更多信息。稍后将进行检查,谢谢-它设计为有一个1角色非常感谢-它起了作用-解决方案是添加personRole=tbls.AspNetUserRoles.Single(m=>m.UserId==UserId);而不是personRole=tbls.AspNetUserRoles.Single(userId);现在我面临另一个问题,但仍然帮了我很大的忙:)我很高兴它帮助了你。试着自己解决另一个问题。如果尝试后无法解决此问题,可以在StackOverflow中再次找到帮助
personRole = tbls.AspNetUserRoles.Single(m => m.UserId == userId);