コード例 #1
0
 public void SaveCustomerCounty(CustomerCounty _customerCounty)
 {
     try
     {
         using (var ctx = new customerDbEntities())
         {
             ctx.CustomerCountys.Add(_customerCounty);
             ctx.Entry(_customerCounty).State = System.Data.Entity.EntityState.Modified;
             ctx.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 btnAdd_Click(object sender, EventArgs e)
        {
            CustomerCounty _newRecord = new CustomerCounty();

            _newRecord.CustomerId        = this.CustomerId;
            _newRecord.CountyCode        = (int)cboCountys.SelectedValue;
            _newRecord.CountyDescription = cboCountys.Text;
            _db.AddToCounties(_newRecord);
            _db.SaveChanges();
            this.Close();
        }
コード例 #3
0
 public void AddToCounties(CustomerCounty newCounty)
 {
     using (var myContext = new customerDbEntities())
     {
         myContext.CustomerCountys.Add(newCounty);
         try
         {
             myContext.SaveChanges();
         }
         catch (DbEntityValidationException ex)
         {
             foreach (var entityValidationErrors in ex.EntityValidationErrors)
             {
                 foreach (var validationError in entityValidationErrors.ValidationErrors)
                 {
                     Console.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                 }
             }
         }
     }
 }