static void Remove() { using (var cxt = new ManyToManyRemoveContext()) { var user = cxt.Users.FirstOrDefault(x => x.Id == 2); if (user != null) { cxt.Users.Remove(user); cxt.SaveChanges(); } } }
static void Update() { using (var cxt = new ManyToManyRemoveContext()) { var user = cxt.Users.FirstOrDefault(x => x.Id == 3); if (user != null) { user.FirstName = "ShuHao"; cxt.SaveChanges(); } } }
static void RemoveManyToMany() { using (var cxt = new ManyToManyRemoveContext()) { var user = cxt.Users.FirstOrDefault(x => x.Id == 1); var roles = new List <Role>(); roles.AddRange(user.Roles.Select(x => x)); foreach (var role in roles) { user.Roles.Remove(role); } cxt.Users.Remove(user); cxt.SaveChanges(); } }
static void Add() { List <Role> roles; using (var cxt = new ManyToManyRemoveContext()) { roles = cxt.Roles.ToList(); cxt.Users.Add(new User { Id = 4, FirstName = "Console", LastName = "App", CreatedOn = DateTime.Now, Roles = roles.Where(x => x.Id == 1).ToList() }); cxt.SaveChanges(); } }