コード例 #1
0
 public static void update(tblStudent c)
 {
     try
     {
         var db = new BusProjectEntities();
         db.Entry <tblStudent>(c).State = EntityState.Modified;
         db.SaveChanges();
     }
     catch (DbEntityValidationException ex)
     {
         var s = ex.EntityValidationErrors.ToList();
     }
 }
コード例 #2
0
        public static void create(tblStudent c)
        {
            try
            {
                BusProjectEntities db = new BusProjectEntities();

                var ids = db.tblStudents.Select(z => z.studentId).ToList();
                var pid = ids.Max(z => Int32.Parse(z)) + 1;
                c.studentId  = pid.ToString();
                c.dateCreate = DateTime.Today;
                c.lastUpdate = DateTime.Today;
                var crYear = tblSystemLogic.getSystemValueByKey("currentRegistrationYear");
                c.yearRegistration   = crYear == null ? DateTime.Now.Year : int.Parse(crYear.strValue);
                c.registrationStatus = false;

                db.tblStudents.Add(c);

                db.SaveChanges();
            }
            //sqlException
            catch (DbEntityValidationException ex)
            {
                foreach (DbEntityValidationResult item in ex.EntityValidationErrors)
                {
                    // Get entry

                    DbEntityEntry entry          = item.Entry;
                    string        entityTypeName = entry.Entity.GetType().Name;

                    // Display or log error messages

                    foreach (DbValidationError subItem in item.ValidationErrors)
                    {
                        string message = string.Format("Error '{0}' occurred in {1} at {2}",
                                                       subItem.ErrorMessage, entityTypeName, subItem.PropertyName);
                        //Console.WriteLine(message);
                    }
                }
            }
            //catch (DbUpdateException ex)
            //{
            //    var sqlex = ex.InnerException.InnerException as SqlException;

            //    if (sqlex != null)
            //    {
            //        switch (sqlex.Number)
            //        {
            //            case 547: throw new ExNoExisteUsuario("No existe usuario destino."); //FK exception
            //            case 2627:
            //            case 2601:
            //                throw new ExYaExisteConexion("Ya existe la conexion."); //primary key exception

            //            default: throw sqlex; //otra excepcion que no controlo.


            //        }
            //    }

            //    throw ex;
            //}
        }