private void FixupInfo(AssociationUserCreditCardInfo previousValue)
     {
         // This is the principal end in an association that performs cascade deletes.
         // Update the event listener to refer to the new dependent.
         if (previousValue != null)
         {
             ChangeTracker.ObjectStateChanging -= previousValue.HandleCascadeDelete;
         }
 
         if (Info != null)
         {
             ChangeTracker.ObjectStateChanging += Info.HandleCascadeDelete;
         }
 
         if (IsDeserializing)
         {
             return;
         }
 
         if (Info != null)
         {
             Info.CardId = Id;
         }
 
         if (ChangeTracker.ChangeTrackingEnabled)
         {
             if (ChangeTracker.OriginalValues.ContainsKey("Info")
                 && (ChangeTracker.OriginalValues["Info"] == Info))
             {
                 ChangeTracker.OriginalValues.Remove("Info");
             }
             else
             {
                 ChangeTracker.RecordOriginalValue("Info", previousValue);
                 // This is the principal end of an identifying association, so the dependent must be deleted when the relationship is removed.
                 // If the current state of the dependent is Added, the relationship can be changed without causing the dependent to be deleted.
                 if (previousValue != null && previousValue.ChangeTracker.State != ObjectState.Added)
                 {
                     previousValue.MarkAsDeleted();
                 }
             }
             if (Info != null && !Info.ChangeTracker.ChangeTrackingEnabled)
             {
                 Info.StartTracking();
             }
         }
     }
     public bool Equals(AssociationUserCreditCardInfo other)
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
 		if (other.CardId == 0 && CardId == 0)
 			return false;
 		else
 			return other.CardId == CardId;
     }
        public AssociationUserCreditCard SaveCreditCard(AssociationUserCreditCard card)
        {
            bool isAdd = card.Id == 0;

            using (var db = new LomsContext())
            {
                db.Connection.Open();

                using (var transaction = db.Connection.BeginTransaction())
                {
                    db.AssociationUserCreditCards.ApplyChanges(card);
                    db.SaveChanges();

                    if (isAdd)
                    {
                        var info = new AssociationUserCreditCardInfo();
                        info.CardId = card.Id;
                        info.Number = card.Number;
                        info.CVC = card.CVC;
                        db.AssociationUserCreditCardInfoes.ApplyChanges(info);
                        db.SaveChanges();
                    }

                    if (card.IsDefaultBilling)
                    {
                        var defaultBilling = db.AssociationUserDefaultBillings.SingleOrDefault(b => b.AssociationUserId == card.AssociationUserId);
                        if (defaultBilling != null && defaultBilling.CreditCardId != card.Id)
                        {
                            defaultBilling.CreditCardId = card.Id;
                            defaultBilling.BillingAccountId = null;
                        }
                        else if (defaultBilling == null)
                            defaultBilling = new AssociationUserDefaultBilling() { AssociationUserId = card.AssociationUserId, CreditCardId = card.Id };

                        db.AssociationUserDefaultBillings.ApplyChanges(defaultBilling);
                        db.SaveChanges();
                    }

                    if (isAdd)
                    {
                        try
                        {
                            var paymentApi = PaymentApi.Get(CurrentAssociationId);
                            paymentApi.PreAuth(card.Number, card.CVC, card.ExpiryYear, card.ExpiryMonth, "cc" + card.Id + "_" + DateTime.UtcNow.ToString("HHmmss"));
                        }
                        catch (Exception ex)
                        {
                            card.AddError("Entity", ex.Message);
                            return card;
                        }
                    }

                    transaction.Commit();
                }
            }

            using (var db = new LomsContext())
            {
                card = db.AssociationUserCreditCards.IncludeAll("Info").FirstOrDefault(a => a.Id == card.Id);
                card.Number = AssociationUserCreditCard.ObfuscateCreditCardNumber(card.Info.Number);
                card.Info = null;
                card.IsDefaultBilling = 0 != db.AssociationUserDefaultBillings.Count(b => b.AssociationUserId == card.AssociationUserId && b.CreditCardId == card.Id);
                card.AcceptChanges();
                return card;
            }
        }
        public AssociationUserCreditCard SaveCreditCard(AssociationUserCreditCard card)
        {
            bool isAdd = card.Id == 0;

            using (var db = new LomsContext())
            {
                db.Connection.Open();

                using (var transaction = db.Connection.BeginTransaction())
                {
                    db.AssociationUserCreditCards.ApplyChanges(card);
                    db.SaveChanges();

                    if (isAdd)
                    {
                        var info = new AssociationUserCreditCardInfo();
                        info.CardId = card.Id;
                        info.Number = card.Number;
                        info.CVC = card.CVC;
                        db.AssociationUserCreditCardInfoes.ApplyChanges(info);
                        db.SaveChanges();
                    }

                    if (card.IsDefaultBilling)
                    {
                        var defaultBilling = db.AssociationUserDefaultBillings.SingleOrDefault(b => b.AssociationUserId == card.AssociationUserId);
                        if (defaultBilling != null && defaultBilling.CreditCardId != card.Id)
                        {
                            defaultBilling.BillingAccountId = null;
                            defaultBilling.CreditCardId = card.Id;
                        }
                        else if (defaultBilling == null)
                            defaultBilling = new AssociationUserDefaultBilling() { AssociationUserId = card.AssociationUserId, CreditCardId = card.Id };

                        db.AssociationUserDefaultBillings.ApplyChanges(defaultBilling);
                        db.SaveChanges();
                    }

                    transaction.Commit();
                }

            }

            using (var db = new LomsContext())
            {
                card = db.AssociationUserCreditCards.IncludeAll("Info").FirstOrDefault(a => a.Id == card.Id);
                card.Number = ObfuscateCreditCardNumber(card.Info.Number);
                card.Info = null;
                card.AcceptChanges();
                return card;
            }
        }