Guest User

Untitled

a guest
Nov 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. var fmResultIds = fmResultset.Resultset.Records.Select(r => r.Fields.First(f => f.Name == primaryKeyName).Value).ToArray();
  2. switch (config.Value.GetTableName())
  3. {
  4. case "FmCompanyIntegration1":
  5. // The FMCompanyIntegration1 should be a parameter
  6. var allsql1 = context.Set<FmCompanyIntegration1>()
  7. .Where(t => true)
  8. //The issue here, the primary key should be a parameter
  9. .Where(t => fmResultIds.Contains(t.CoId))
  10. .Take(100000)
  11. .ToDictionary(kvp => kvp.CoId, kvp => kvp);
  12. //this is taking care to update/insert the record. We do not have to delete anything
  13. foreach (var record in fmResultset.Resultset.Records)
  14. {
  15. var entity = allsql1.FirstOrDefault(r => r.Key == record.Fields.First(f => f.Name == primaryKeyName).Value).Value;
  16. if (entity != null)
  17. {
  18. // LoadRecord is not a problem, because all entity is comming from the same base class, which has this function defined
  19. entity.LoadRecord(record);
  20. context.Entry(entity).State = EntityState.Modified;
  21. updated++;
  22. }
  23. else
  24. {
  25. // here I also have to dynamicaly create the class...
  26. entity = new FmCompanyIntegration1();
  27. entity.LoadRecord(record);
  28. context.Entry(entity).State = EntityState.Added;
  29. inserted++;
  30. }
  31. }
  32. break;
  33.  
  34. // full copypaste from above. I would like to get rid of this obviously
  35. case "FmBookBackyard1":
  36. default:
  37. var allsql2 = context.Set<FmBookBackyard1>()
  38. .Where(t => true)
  39. // Again I know what is the primary key field name... Or I am ready to extend any reasonable class to give it back
  40. .Where(t => fmResultIds.Contains(t.BkRecordId))
  41. .Take(100000)
  42. .ToDictionary(kvp => kvp.BkRecordId, kvp => kvp);
  43. //this is taking care to update/insert the record. We do not have to delete anything
  44. foreach (var record in fmResultset.Resultset.Records)
  45. {
  46. var entity = allsql2.FirstOrDefault(r => r.Key == record.Fields.First(f => f.Name == primaryKeyName).Value).Value;
  47. if (entity != null)
  48. {
  49. entity.LoadRecord(record);
  50. context.Entry(entity).State = EntityState.Modified;
  51. updated++;
  52. }
  53. else
  54. {
  55. entity = new FmBookBackyard1();
  56. entity.LoadRecord(record);
  57. context.Entry(entity).State = EntityState.Added;
  58. inserted++;
  59. }
  60. }
  61. break;
  62. }
  63. context.SaveChanges();
Add Comment
Please, Sign In to add comment