In my application, there is an AddFriend action, in which I add one user to the friend’s list of another user, update the data in the database and save the changes, but later the changes are not visible.

Used model:

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace SocialNetwork.Models { public class UserModel { public int Id { get; set; } public string Name { get; set; } public string Password { get; set; } public string Sex { get; set; } public List<UserModel> Friends { get; set; } } } 

HomeController class (adding a user as a friend in the AddFriend action, in which I pass the id of the user I want to add as a friend):

 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SocialNetwork.Models; namespace SocialNetwork.Controllers { [Authorize] public class HomeController : Controller { SocialNetworkContext db; public HomeController(SocialNetworkContext context) { db = context; } public IActionResult Index() { var user = db.Users.First(u => u.Name == HttpContext.User.Identity.Name); return Redirect($"~/Home/UserProfile/{user.Id}"); } public IActionResult UserProfile(int Id) { ViewBag.is_auth_user = IsAuthorizedUser(Id); var user = db.Users.First(u => u.Id == Id); return View(user); } public IActionResult AddFriend(int Id) { var user = db.Users.First(u => u.Name == HttpContext.User.Identity.Name); var new_friend = db.Users.First(u => u.Id == Id); if (user.Friends == null) { user.Friends = new List<UserModel>(); } user.Friends.Add(new_friend); db.Users.Update(user); db.SaveChanges(); return Redirect($"~/Home/UserProfile/{new_friend.Id}"); } public IActionResult Users() { return View(db.Users.ToList()); } [HttpGet] public IActionResult Edit(int Id) { var user = db.Users.FirstOrDefault(u => u.Id == Id); return View(user); } [HttpPost] public IActionResult Edit(UserModel user) { db.Users.Update(user); db.SaveChanges(); return Redirect($"~/Home/UserProfile/{user.Id}"); } protected bool IsAuthorizedUser(int Id) { var user = db.Users.First(u => u.Name == HttpContext.User.Identity.Name); if (user.Id == Id) { return true; } else { return false; } } } } 

I add two users (logged in under us2): enter image description here

Go to us1 user page and add it to friends: enter image description here

When you go back to my page, the friends list is still empty: enter image description here

But when debugging (as shown below), if I enter my database, go to the list with the current user’s friends, it turns out that it is not at all empty and they will be displayed (if you do not enter the database, users will not be displayed): enter image description here enter image description here

  • As I understand it, they are recording in the database and they are constantly there, just not being displayed on the page? - FX_Sektor
  • @FX_Sektor Not really, when calling one action they are, when calling another - no. For example, when I launch the AddFriend action (I add to friends), then the user (user in the code) to whom I want to add a friend already has a friend (this is the case when I ran the AddFriend action before). But when I then go to the page of my user, to whom I have already added a friend (the Index action is invoked, then redirects to the UserProfile action), then the list with friends (List <UserModel> Friends) is null. - Anton Belsky
  • When you call the UserProfile method from the Index method, then you have a context, the db that you initialize when the controller starts up is reset, is this a problem? - FX_Sektor
  • And yet, are you sure that the UserProfile method accepts a non-null id? Here either the db context is set to zero, or null comes to the UserProfile method - FX_Sektor
  • @FX_Sektor The id is passed to the UserProfile method, everything is ok with this. And how can db be reset and where? And there is how to fix it? - Anton Belsky

1 answer 1

This is a lazy loading problem in EF. Entities do not load until I access them. It is necessary to use Include("Friends") when loading a user in the UserProfile() method. This will ensure that the related objects are loaded.

That is, you need to replace this line: var user = db.Users.First(u => u.Id == Id);

On this: var user = db.Users.Include("Friends").First(u => u.Id == Id);