internal protected virtual int AddOrUpdate(int roleId, List <AuthType> list, FileContext db) { int count = 0; if (list != null && roleId > 0) { list = new List <AuthType>(list); db.Configuration.AutoDetectChangesEnabled = true; var old = db.RoleAuth.Where(q => q.RoleId == roleId).ToList(); List <RoleAuth> del = new List <RoleAuth>(); while (old.Count > 0) { var m = old[0]; old.Remove(m); if (list.Contains(m.Type)) { list.Remove(m.Type); } else { del.Add(m); } } db.AddCommitCallback(() => { this.GetCache <IRoleAuthCache>().Remove(roleId); }); while (list.Count > 0) { var auth = list[0]; list.Remove(auth); RoleAuth m = null; if (del.Count > 0) { m = del[0]; del.Remove(m); } else { m = new RoleAuth() { RoleId = roleId }; } m.Type = auth; if (m.Id == 0) { db.RoleAuth.Add(m); } count += db.SaveChanges(); } del.ForEach(m => { db.Entry(m).State = System.Data.Entity.EntityState.Deleted; count += db.SaveChanges(); }); } return(count); }
internal protected virtual int AddOrUpdate(int serverId, List <SyncType> list, FileContext db) { int count = 0; if (list != null && serverId > 0) { list = new List <SyncType>(list); db.Configuration.AutoDetectChangesEnabled = true; var old = db.ServerSyncType.Where(q => q.ServerId == serverId).ToList(); List <ServerSyncType> del = new List <ServerSyncType>(); while (old.Count > 0) { var m = old[0]; old.Remove(m); if (list.Contains(m.Type)) { list.Remove(m.Type); } else { del.Add(m); } } db.AddCommitCallback(() => { var serverSyncTypeCache = this.GetCache <IServerSyncTypeCache>(); serverSyncTypeCache.Remove(serverId); }); while (list.Count > 0) { var syncType = list[0]; list.Remove(syncType); ServerSyncType m = null; if (del.Count > 0) { m = del[0]; del.Remove(m); } else { m = new ServerSyncType() { ServerId = serverId }; } m.Type = syncType; if (m.Id == 0) { db.ServerSyncType.Add(m); } count += db.SaveChanges(); } del.ForEach(m => { db.Entry(m).State = System.Data.Entity.EntityState.Deleted; count += db.SaveChanges(); }); } return(count); }