Пример #1
0
 static void Query()
 {
     using (var cxt = new ManyToManyRemoveContext())
     {
         var users = cxt.Users.ToList();
         users.ForEach(x =>
         {
             Console.WriteLine("User First Name:{0},Last Name:{1},Create On:{2}\n |__Roles:{3}", x.FirstName, x.LastName, x.CreatedOn, string.Join(",", x.Roles.Select(r => r.Name)));
         });
     }
 }
Пример #2
0
 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();
         }
     }
 }
Пример #3
0
 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();
         }
     }
 }
Пример #4
0
 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();
     }
 }
Пример #5
0
        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();
            }
        }