static void Execute_NG() { using (var db = new ManyToManyDbEntities()) { //讀取資料 var users = db.Users.ToList(); var roles = db.Roles.ToList(); var userRoles = db.UserRoles.ToList(); var userRolesAdd = userRoles.Select(x => new UserRole() { UserId = x.UserId, RoleId = x.RoleId }).ToList(); //刪除資料 db.UserRoles.RemoveRange(userRoles); db.Roles.RemoveRange(roles); db.Users.RemoveRange(users); //db.Database.Log = Console.WriteLine; //db.SaveChanges(); Console.WriteLine("Press any key to continue (1)..."); Console.ReadLine(); //加入資料 db.Users.AddRange(users); db.Roles.AddRange(roles); db.UserRoles.AddRange(userRolesAdd); db.Database.Log = Console.WriteLine; db.SaveChanges(); Console.WriteLine("Press any key to continue (2)..."); Console.ReadLine(); } }
static void DisplayAll() { using (var db = new ManyToManyDbEntities()) { // db.Database.Log = Console.WriteLine; foreach (var user in db.Users) { Console.WriteLine($"Id:{user.UserId} Name:{user.UserName}"); } foreach (var role in db.Roles) { Console.WriteLine($"Id:{role.RoleId} Name:{role.RoleName}"); } foreach (var item in db.UserRoles) { Console.WriteLine($"Id:{item.Id} UserId:{item.UserId} RoleId:{item.RoleId}"); } Console.WriteLine("Press any key to continue ..."); Console.ReadLine(); //var users = new List<User>() //{ // new User() { UserId = "User01", UserName="******"}, // new User() { UserId = "User02", UserName = "******"} //}; //var roles = new List<Role>() //{ // new Role() { RoleId = "Role01", RoleName = "Project Manager"}, // new Role() { RoleId = "Role02", RoleName = "Developer"}, // new Role() { RoleId = "Role03", RoleName = "DBA"}, //}; //var userRoles = new List<UserRole>() //{ // new UserRole() { UserId = "User01", RoleId = "Role02"}, // new UserRole() { UserId = "User01", RoleId = "Role03"}, // new UserRole() { UserId = "User02", RoleId = "Role01"}, // new UserRole() { UserId = "User02", RoleId = "Role02"}, //}; } }
static void Execute_OK_With_New_PK() { using (var db = new ManyToManyDbEntities()) { //讀取資料 var users = db.Users.ToList(); var roles = db.Roles.ToList(); var userRoles = db.UserRoles.ToList(); //3 個 table 都給新的 PK 值 var usersAdd = users.Select(x => new User() { UserId = x.UserId + "N", UserName = x.UserName }).ToList(); var rolesAdd = roles.Select(x => new Role() { RoleId = x.RoleId + "N", RoleName = x.RoleName }).ToList(); var userRolesAdd = userRoles.Select(x => new UserRole() { UserId = x.UserId + "N", RoleId = x.RoleId + "N" }).ToList(); //刪除資料 db.UserRoles.RemoveRange(userRoles); db.Roles.RemoveRange(roles); db.Users.RemoveRange(users); //db.Database.Log = Console.WriteLine; //db.SaveChanges(); Console.WriteLine("Press any key to continue (3)..."); Console.ReadLine(); //加入資料 db.Users.AddRange(usersAdd); db.Roles.AddRange(rolesAdd); db.UserRoles.AddRange(userRolesAdd); db.Database.Log = Console.WriteLine; db.SaveChanges(); Console.WriteLine("Press any key to continue (4)..."); Console.ReadLine(); } }
static void Execute_OK_With_TransactionScope() { using (var db = new ManyToManyDbEntities()) { //讀取資料 var users = db.Users.ToList(); var roles = db.Roles.ToList(); var userRoles = db.UserRoles.ToList(); var userRolesAdd = userRoles.Select(x => new UserRole() { UserId = x.UserId, RoleId = x.RoleId }).ToList(); using (TransactionScope ts = new TransactionScope()) { //刪除資料 db.UserRoles.RemoveRange(userRoles); db.Roles.RemoveRange(roles); db.Users.RemoveRange(users); db.Database.Log = Console.WriteLine; db.SaveChanges(); Console.WriteLine("Press any key to continue (5)..."); Console.ReadLine(); //加入資料 db.Users.AddRange(users); db.Roles.AddRange(roles); db.UserRoles.AddRange(userRolesAdd); db.Database.Log = Console.WriteLine; db.SaveChanges(); Console.WriteLine("Press any key to continue (6)..."); Console.ReadLine(); ts.Complete(); } } }