尝试添加/编辑applicationUser的相关数据属性,以便可以为用户记录注释
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
public virtual ICollection<UserNote> UserNotes { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
}
UserNote.cs
public class UserNote
{
[Key]
public int UserNoteId { get; set; }
public string Message { get; set; }
public string ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
控制器
[HttpPost,actionName("Edit")]
[ValidateAntiForgeryToken]
public async Task<IactionResult> EditPost(string id,ApplicationUser applicationUser)
{
if (ModelState.IsValid)
{
var userFromDb = _db.ApplicationUser
.Include(n => n.UserNotes)
.Include(r => r.UserRoles)
.AsnoTracking().FirstOrDefault(m => m.Id == id);
UserNote note = _db.UserNote.Single(x => x.UserNoteId == 1);
userFromDb.LastName = applicationUser.LastName;
userFromDb.FirstName = applicationUser.FirstName;
//Error accessing related data properties
userFromDb.UserNotes.Message = applicationUser.UserNotes.Message;
await _db.SaveChangesAsync();
return RedirectToaction(nameof(Index));
}
return View(applicationUser);
}
我收到的'ICollection'不包含'Message'的定义,并且没有可访问的扩展方法'Message'接受类型为'ICollection'的第一个参数