Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. public void SaveRecipient(Recipient myRecipient)
  2. {
  3. if (myRecipient.RecipientGUID == Guid.Empty)
  4. {
  5. myRecipient.RecipientGUID = Guid.NewGuid();
  6.  
  7. foreach (ContactMethod tmpCM in myRecipient.ContactMethods)
  8. {
  9. context.Entry(tmpCM.Type).State = EntityState.Unchanged;
  10. }
  11.  
  12. context.Entry(myRecipient.LastModifiedBy).State = EntityState.Unchanged;
  13. context.Entry(myRecipient.Owner).State = EntityState.Unchanged;
  14. context.Entry(myRecipient.CreatedBy).State = EntityState.Unchanged;
  15.  
  16. context.Recipients.Add(myRecipient);
  17. }
  18. else
  19. {
  20. var dbRecipient = context.Recipients
  21. .Include(a => a.ContactMethods).ThenInclude(t => t.Type)
  22. .Include(b => b.CreatedBy)
  23. .Include(c => c.LastModifiedBy)
  24. .Include(d => d.Owner).ThenInclude(o => o.Users)
  25. .FirstOrDefault(x => x.RecipientGUID == myRecipient.RecipientGUID);
  26.  
  27. if (dbRecipient != null)
  28. {
  29. dbRecipient.FirstName = myRecipient.FirstName;
  30. dbRecipient.LastName = myRecipient.LastName;
  31. dbRecipient.Company = myRecipient.Company;
  32.  
  33. foreach (ContactMethod tmpCM in myRecipient.ContactMethods)
  34. {
  35. var dbCM = dbRecipient.ContactMethods.FirstOrDefault(x => x.ContactMethodGUID == tmpCM.ContactMethodGUID);
  36.  
  37. if (dbCM != null)
  38. {
  39. dbCM.CountryCode = tmpCM.CountryCode;
  40. dbCM.Identifier = tmpCM.Identifier;
  41. dbCM.IsPreferred = tmpCM.IsPreferred;
  42. }
  43. else
  44. {
  45. dbRecipient.ContactMethods.Add(tmpCM);
  46. }
  47. }
  48.  
  49. //Only update this if it has changed.
  50. if (dbRecipient.LastModifiedBy.UserGUID != myRecipient.LastModifiedBy.UserGUID)
  51. {
  52. dbRecipient.LastModifiedBy = myRecipient.LastModifiedBy;
  53. }
  54.  
  55. dbRecipient.LastModifiedOn = myRecipient.LastModifiedOn;
  56. }
  57. }
  58.  
  59. context.SaveChanges();
  60. }
  61.  
  62. public class User
  63. {
  64. [Key]
  65. public Guid UserGUID { get; set; }
  66.  
  67. public string UserName { get; set; }
  68.  
  69. public string FirstName { get; set; }
  70.  
  71. public string LastName { get; set; }
  72.  
  73. public string Email { get; set; }
  74.  
  75. public bool IsSiteAdmin { get; set; }
  76.  
  77. public bool IsActive { get; set; }
  78.  
  79. public DateTime? CreatedOn { get; set; }
  80.  
  81. public DateTime? LastLogin { get; set; }
  82. }
  83.  
  84. public class Recipient
  85. {
  86. [Key]
  87. public Guid RecipientGUID { get; set; }
  88.  
  89. [Required(ErrorMessage = "Please enter a Recipient's First Name.")]
  90. public string FirstName { get; set; }
  91.  
  92. [Required(ErrorMessage = "Please enter a Recipient's Last Name.")]
  93. public string LastName { get; set; }
  94.  
  95. public string Company { get; set; }
  96.  
  97. public UserGroup Owner { get; set; }
  98.  
  99. public virtual ICollection<ContactMethod> ContactMethods { get; set; }
  100.  
  101. public User CreatedBy { get; set; }
  102.  
  103. public DateTime CreatedOn { get; set; }
  104.  
  105. public User LastModifiedBy { get; set; }
  106.  
  107. public DateTime LastModifiedOn { get; set; }
  108.  
  109. public bool IsActive { get; set; }
  110. }
  111.  
  112. public class ContactMethod
  113. {
  114. [Key]
  115. [HiddenInput(DisplayValue = false)]
  116. public Guid ContactMethodGUID { get; set; }
  117.  
  118. [ForeignKey("ContactMethodTypeGUID")]
  119. public virtual ContactMethodType Type { get; set; }
  120.  
  121. public string CountryCode { get; set; }
  122.  
  123. [Required]
  124. public string Identifier { get; set; }
  125.  
  126. public bool IsPreferred { get; set; }
  127.  
  128. [ForeignKey("RecipientGUID")]
  129. public virtual Recipient Owner { get; set; }
  130. }
  131.  
  132. context.Entry(myRecipient.LastModifiedBy).State = EntityState.Unchanged;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement