gpt4 book ai didi

c# - ASP.NET - 使用字符串 ID 和 2 个键查询表

转载 作者:行者123 更新时间:2023-11-28 23:26:39 25 4
gpt4 key购买 nike

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

Controller :

 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 键入)。然后在 View 中,我希望能够获得针对 AspNetUserRoles 键入的特定记录并能够对其进行编辑,尽管现在它无关紧要,因为我在查询数据库时无法获得正确的记录。

我的想法是,这要么是使用 @Html.ActionLink("Edit", "Edit", "Home", new { id = item.Id }, null) 的路由问题(检查了 URI 的样子,它是正确的意思是 Controller / Action /参数没问题,在调试期间字符串 Id 不为空)或者 Find() 方法有问题,因为它需要字符串参数或者我正在查询具有 2 个键的表的事实(虽然它只是一个想法,没有什么实际意义)。

任何线索都可能有用。

谢谢!

最佳答案

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

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

roleId 是你要找的角色的id。如果您只有一个与用户关联的角色,那么您可以查询并获取它:

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

但请记住,如果用户拥有多个角色,上述代码将抛出异常。

阅读有关查找实体的更多信息 here .

关于c# - ASP.NET - 使用字符串 ID 和 2 个键查询表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39074158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com