public void MergeProfiles(Profile targetProfile, RcfProfile rcfProfile, FideProfile fideProfile) { using (var db = new Ri2Context()) { targetProfile = db.Profiles.FirstOrDefault(x => x.Id == targetProfile.Id); if (targetProfile == null) { return; } if (rcfProfile != null) { rcfProfile = db.RcfProfiles.FirstOrDefault(x => x.Id == rcfProfile.Id); } if (fideProfile != null) { fideProfile = db.FideProfiles.FirstOrDefault(x => x.Id == fideProfile.Id); } if (rcfProfile != null) { targetProfile.RcfProfile = rcfProfile; } if (fideProfile != null) { targetProfile.FideProfile = fideProfile; if (rcfProfile != null) { rcfProfile.FideProfile = fideProfile; } } db.SaveChanges(); } }
private static void ProcessRcf() { var fi = new FileInfo(RcfFilePath); using (var pkg = new ExcelPackage(fi)) { var ws = pkg.Workbook.Worksheets[1]; var start = ws.Dimension.Start; var end = ws.Dimension.End; var add = new List <RcfProfile>(75000); var mod = new List <RcfProfile>(75000); Dictionary <int, int> tf; Dictionary <int, int> t; using (var db = new Ri2Context { Configuration = { AutoDetectChangesEnabled = false } }) { tf = db.FideProfiles.Select(x => new { x.FideId, x.Id }) .ToDictionary(o => o.FideId, o => o.Id); t = db.RcfProfiles.Select(x => new { x.RcfId, x.Id }) .ToDictionary(o => o.RcfId, o => o.Id); } for (var i = start.Row + 1; i <= end.Row; i++) { var pr = new RcfProfile { RcfId = ws.Cells[i, RcfColumns.RcfId].GetValue <int>(), Name = ws.Cells[i, RcfColumns.Name].GetValue <string>(), Birth = ws.Cells[i, RcfColumns.Birth].GetValue <string>() != "" ? ws.Cells[i, RcfColumns.Birth].GetValue <int>() : 0, Std = ws.Cells[i, RcfColumns.Std].GetValue <string>() != "" ? ws.Cells[i, RcfColumns.Std].GetValue <int>() : 0, Rpd = ws.Cells[i, RcfColumns.Rpd].GetValue <string>() != "" ? ws.Cells[i, RcfColumns.Rpd].GetValue <int>() : 0, Blz = ws.Cells[i, RcfColumns.Blz].GetValue <string>() != "" ? ws.Cells[i, RcfColumns.Blz].GetValue <int>() : 0 }; if (pr.Birth < Settings.Current.BirthCutoff) { continue; } var fideId = ws.Cells[i, RcfColumns.FideId].GetValue <int?>(); //using (var ri2 = new Ri2Context {Configuration = {AutoDetectChangesEnabled = false}}) { if (fideId.HasValue) { if (tf.ContainsKey(fideId.Value)) { pr.FideProfileId = tf[fideId.Value]; } } //var t = ri2.RcfProfiles.FirstOrDefault(cp => cp.RcfId == pr.RcfId); if (t.ContainsKey(pr.RcfId)) { pr.Id = t[pr.RcfId]; mod.Add(pr); } else { add.Add(pr); } } } using (var ri2 = new Ri2Context()) { EFBatchOperation.For(ri2, ri2.RcfProfiles).InsertAll(add); EFBatchOperation.For(ri2, ri2.RcfProfiles).UpdateAll(mod, x => x.ColumnsToUpdate(c => c.Name, c => c.Birth, c => c.Std, c => c.Rpd, c => c.Blz, c => c.FideProfileId)); ri2.SaveChanges(); } } }