我尝试了很多选项并将其包含在此处。我试过的任何选项似乎都会产生一些新错误。
public async Task<ActionResult> Index()
{
//var userId = UserManager.FindById(User.Identity.GetUserId());
//var requests = db.Requests.Where(m => m.UserId == userId); ;
//var requests = db.Requests.Include(r => r.User);
//return View(await requests.ToListAsync());
//var userId = UserManager.FindById(User.Identity.GetUserId());
//var requests = db.Requests.Where(m => m.UserId == User.Identity.GetUserId());
//return View(await requests.ToListAsync());
var requests = db.Requests.Include(e => e.User).Where(m => m.UserId == User.Identity.GetUserId());
return View(requests);
}
和我的模特
public class Request
{
[Key]
public int RequestId { get; set; }
public string UserId { get; set; }
[StringLength(128), MinLength(3)]
[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
public DateTime Date
{
get { return DateTime.Now; }
}
public string Ticket { get; set; }
}
我终于找到了如何创建与当前用户关联的记录,但我无法仅查看与当前用户相关的记录。
错误是
LINQ to Entities无法识别方法&#39; System.String GetUserId(System.Security.Principal.IIdentity)&#39;方法,并且此方法无法转换为商店表达式。
答案 0 :(得分:2)
首先将当前用户的值分配给变量,然后在查询中使用该值
string userID = User.Identity.GetUserId();
var requests = db.Requests.Include(e => e.User).Where(m => m.UserId == userID);
return View(requests);