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): 
Go to us1 user page and add it to friends: 
When you go back to my page, the friends list is still empty: 
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):
