Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void SaveRecipient(Recipient myRecipient)
- {
- if (myRecipient.RecipientGUID == Guid.Empty)
- {
- myRecipient.RecipientGUID = Guid.NewGuid();
- foreach (ContactMethod tmpCM in myRecipient.ContactMethods)
- {
- context.Entry(tmpCM.Type).State = EntityState.Unchanged;
- }
- context.Entry(myRecipient.LastModifiedBy).State = EntityState.Unchanged;
- context.Entry(myRecipient.Owner).State = EntityState.Unchanged;
- context.Entry(myRecipient.CreatedBy).State = EntityState.Unchanged;
- context.Recipients.Add(myRecipient);
- }
- else
- {
- var dbRecipient = context.Recipients
- .Include(a => a.ContactMethods).ThenInclude(t => t.Type)
- .Include(b => b.CreatedBy)
- .Include(c => c.LastModifiedBy)
- .Include(d => d.Owner).ThenInclude(o => o.Users)
- .FirstOrDefault(x => x.RecipientGUID == myRecipient.RecipientGUID);
- if (dbRecipient != null)
- {
- dbRecipient.FirstName = myRecipient.FirstName;
- dbRecipient.LastName = myRecipient.LastName;
- dbRecipient.Company = myRecipient.Company;
- foreach (ContactMethod tmpCM in myRecipient.ContactMethods)
- {
- var dbCM = dbRecipient.ContactMethods.FirstOrDefault(x => x.ContactMethodGUID == tmpCM.ContactMethodGUID);
- if (dbCM != null)
- {
- dbCM.CountryCode = tmpCM.CountryCode;
- dbCM.Identifier = tmpCM.Identifier;
- dbCM.IsPreferred = tmpCM.IsPreferred;
- }
- else
- {
- dbRecipient.ContactMethods.Add(tmpCM);
- }
- }
- //Only update this if it has changed.
- if (dbRecipient.LastModifiedBy.UserGUID != myRecipient.LastModifiedBy.UserGUID)
- {
- dbRecipient.LastModifiedBy = myRecipient.LastModifiedBy;
- }
- dbRecipient.LastModifiedOn = myRecipient.LastModifiedOn;
- }
- }
- context.SaveChanges();
- }
- public class User
- {
- [Key]
- public Guid UserGUID { get; set; }
- public string UserName { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Email { get; set; }
- public bool IsSiteAdmin { get; set; }
- public bool IsActive { get; set; }
- public DateTime? CreatedOn { get; set; }
- public DateTime? LastLogin { get; set; }
- }
- public class Recipient
- {
- [Key]
- public Guid RecipientGUID { get; set; }
- [Required(ErrorMessage = "Please enter a Recipient's First Name.")]
- public string FirstName { get; set; }
- [Required(ErrorMessage = "Please enter a Recipient's Last Name.")]
- public string LastName { get; set; }
- public string Company { get; set; }
- public UserGroup Owner { get; set; }
- public virtual ICollection<ContactMethod> ContactMethods { get; set; }
- public User CreatedBy { get; set; }
- public DateTime CreatedOn { get; set; }
- public User LastModifiedBy { get; set; }
- public DateTime LastModifiedOn { get; set; }
- public bool IsActive { get; set; }
- }
- public class ContactMethod
- {
- [Key]
- [HiddenInput(DisplayValue = false)]
- public Guid ContactMethodGUID { get; set; }
- [ForeignKey("ContactMethodTypeGUID")]
- public virtual ContactMethodType Type { get; set; }
- public string CountryCode { get; set; }
- [Required]
- public string Identifier { get; set; }
- public bool IsPreferred { get; set; }
- [ForeignKey("RecipientGUID")]
- public virtual Recipient Owner { get; set; }
- }
- context.Entry(myRecipient.LastModifiedBy).State = EntityState.Unchanged;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement