示例#1
0
        public void AddDealRecord(DealRecord deal)
        {
            MasterFI            mFI       = new MasterFI();
            IList <PropertyMap> propItems = GetPropertyMap(deal.GetType(), mFI.GetType());

            foreach (var prop in propItems)
            {
                var sourceValue = prop.DealProperty.GetValue(deal, null);
                prop.MasterProperty.SetValue(mFI, sourceValue, null);
            }

            context.MasterFIs.Add(mFI);
            try
            {
                context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }
示例#2
0
        private void CreateRoles()
        {
            user = context.Users.Where(u => u.Email == user.Email).First();

            var role = new XRef_UserDealershipRoles
            {
                UserID       = user.ID,
                DealershipID = 99,
                RoleID       = 1,
                CreatedBy    = "*****@*****.**",
                CreatedDate  = DateTime.Now
            };

            context.XRef_UserDealershipRoles.Add(role);

            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                                                       validationErrors.Entry.Entity.ToString(),
                                                       validationError.ErrorMessage);
                        // raise a new exception nesting
                        // the current instance as InnerException
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }
        }