Example #1
0
 private void Remove()
 {
     using (var cxt = new ManyToManyRemoveContext())
     {
         var user = cxt.Users.FirstOrDefault(x => x.LastName.ToString().ToUpper() == "LIN");
         cxt.Users.Remove(user);
         cxt.SaveChanges();
     }
 }
Example #2
0
 private 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)));
         });
     }
 }
Example #3
0
 private void RemoveManyToMany()
 {
     using (var cxt = new ManyToManyRemoveContext())
     {
         var user  = cxt.Users.FirstOrDefault(x => x.LastName.ToString().ToUpper() == "LIN");
         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();
     }
 }
Example #4
0
        private void Add()
        {
            List <Role> roles;

            using (var cxt = new ManyToManyRemoveContext())
            {
                roles = cxt.Roles.ToList();
                cxt.Users.Add(new User
                {
                    Id        = 3,
                    FirstName = "jerimy",
                    LastName  = "Lin",
                    CreatedOn = DateTime.Now,
                    Roles     = roles.Where(x => x.Name == "管理员").ToList()
                });
                cxt.SaveChanges();
            }
        }
Example #5
0
        private void button6_Click(object sender, EventArgs e)
        {
            using (var cxt = new ManyToManyRemoveContext())
            {
                List <User> User_List = cxt.Users.ToList();

                foreach (var item in User_List)
                {
                    cxt.Users.Remove(item);
                }

                List <Role> Role_List = cxt.Roles.ToList();

                foreach (var item in Role_List)
                {
                    cxt.Roles.Remove(item);
                }

                cxt.SaveChanges();
            }
        }