Ejemplo n.º 1
0
 internal int SaveChanges()
 {
     try
     {
         return(_db.SaveChanges());
     }
     catch (DbUpdateConcurrencyException ex)
     {
         //Thrown when there is a concurrency error
         //for now, just rethrow the exception
         throw;
     }
     catch (RetryLimitExceededException ex)
     {
         //Thrown when max retries have been hit
         //Examine the inner exception(s) for additional details
         //for now, just rethrow the exception
         throw;
     }
     catch (DbUpdateException ex)
     {
         //Thrown when database update fails
         //Examine the inner exception(s) for additional
         //details and affected objects
         //for now, just rethrow the exception
         throw;
     }
     catch (Exception ex)
     {
         //some other exception happened and should be handled
         throw;
     }
 }
Ejemplo n.º 2
0
 internal int SaveChanges()
 {
     try
     {
         return(_db.SaveChanges());
     }
     catch (DbUpdateConcurrencyException ex)
     {
         //Генерируется, когда возникла ошибка, связанная с параллелизмом.
         //Пока что просто сгенерировать исключение повторно
         throw;
     }
     catch (RetryLimitExceededException ex)
     {
         //Генерируется, когда достигнуто максимальное количество попыток.
         //Дополнительные детали можно найти во внутреннем исключении (исключениях).
         //Пока что просто сгенерировать исключение повторно.
         throw;
     }
     catch (DbUpdateException ex)
     {
         //Генерируется, когда обновление базы данных потерпело неудачу.
         //Доп. детали и затронутые объекты можно найти во внутреннем исключении.
         //Пока что просто сгенерировать исключение повторно
         throw;
     }
     catch (Exception ex)
     {
         //Возникло какое-то другое исключение, которое должно быть обработано.
         throw;
     }
 }
Ejemplo n.º 3
0
 private int SaveChanges()
 {
     try
     {
         return(_db.SaveChanges());
     }
     catch (DbUpdateConcurrencyException ex)
     {
         throw;
     }
     catch (DbUpdateException ex)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw;
     }
 }
Ejemplo n.º 4
0
 internal int SaveChages()
 {
     try
     {
         return(_db.SaveChanges());
     }
     catch (DbUpdateConcurrencyException ex)
     {
         throw;
     }
     catch (RetryLimitExceededException ex)
     {
         throw;
     }
     catch (DbUpdateException ex)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw;
     }
 }
Ejemplo n.º 5
0
 internal int SaveChanges()
 {
     try
     {
         return(_db.SaveChanges());
     }
     catch (DbUpdateConcurrencyException e)
     {
         Console.WriteLine(e);
         throw;
     }
     catch (RetryLimitExceededException)
     {
         throw;
     }
     catch (DbUpdateException)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw;
     }
 }
Ejemplo n.º 6
0
 internal int SaveChanges()
 {
     try
     {
         return(_db.SaveChanges());
     }
     catch (DbUpdateConcurrencyException ex)
     {
         //Thrown when there is a concurrency error
         throw;
     }
     catch (RetryLimitExceededException ex)
     {
         throw;
     }
     catch (DbUpdateException ex)
     {
         throw;
     }
     catch (Exception ex)
     {
         throw;
     }
 }
        public static void InitializeData(AutoLotContext context)
        {
            var customers = new List <Customer>
            {
                new Customer {
                    FirstName = "Dave", LastName = "Brenner"
                },
                new Customer {
                    FirstName = "Matt", LastName = "Walton"
                },
                new Customer {
                    FirstName = "Steve", LastName = "Hagen"
                },
                new Customer {
                    FirstName = "Pat", LastName = "Walton"
                },
                new Customer {
                    FirstName = "Bad", LastName = "Customer"
                }
            };

            customers.ForEach(x => context.Customers.Add(x));
            context.SaveChanges();
            var cars = new List <Inventory>
            {
                new Inventory {
                    Make = "VW", Color = "Black", PetName = "Zippy"
                },
                new Inventory {
                    Make = "Ford", Color = "Rust", PetName = "Rusty"
                },
                new Inventory {
                    Make = "Saab", Color = "Black", PetName = "Mel"
                },
                new Inventory {
                    Make = "Yugo", Color = "Yellow", PetName = "Clunker"
                },
                new Inventory {
                    Make = "BMW", Color = "Black", PetName = "Bimmer"
                },
                new Inventory {
                    Make = "BMW", Color = "Green", PetName = "Hank"
                },
                new Inventory {
                    Make = "BMW", Color = "Pink", PetName = "Pinky"
                },
                new Inventory {
                    Make = "PInto", Color = "Black", PetName = "Pete"
                },
                new Inventory {
                    Make = "Yugo", Color = "Brown", PetName = "Brownie"
                },
            };

            context.Cars.AddRange(cars);
            context.SaveChanges();
            var orders = new List <Order>
            {
                new Order {
                    Car = cars[0], Customer = customers[0]
                },
                new Order {
                    Car = cars[1], Customer = customers[1]
                },
                new Order {
                    Car = cars[2], Customer = customers[2]
                },
                new Order {
                    Car = cars[3], Customer = customers[3]
                },
            };

            orders.ForEach(x => context.Orders.Add(x));
            context.SaveChanges();
            context.CreditRisks.Add(
                new CreditRisk
            {
                Id        = customers[4].Id,
                FirstName = customers[4].FirstName,
                LastName  = customers[4].LastName
            });
            context.Database.OpenConnection();
            try
            {
                var tableName    = context.GetTableName(typeof(CreditRisk));
                var rawSqlString = $"SET IDENTITY_INSERT dbo.{tableName} ON;";
                context.Database.ExecuteSqlCommand(rawSqlString);
                context.SaveChanges();
                rawSqlString = $"SET IDENTITY_INSERT dbo.{tableName} OFF";
                context.Database.ExecuteSqlCommand(rawSqlString);
            }
            finally
            {
                context.Database.CloseConnection();
            }
        }
        public static void InitializeData(AutoLotContext context)
        {
            var customers = new List <Customer>
            {
                new Customer {
                    FirstName = "Dave", LastName = "Brener"
                },
                new Customer {
                    FirstName = "Matt", LastName = "Walton"
                },
                new Customer {
                    FirstName = "Steve", LastName = "Hagen"
                },
                new Customer {
                    FirstName = "Pat", LastName = "Walton"
                },
                new Customer {
                    FirstName = "Bad", LastName = "Customer"
                }
            };

            customers.ForEach(x => context.Customers.Add(x));
            context.SaveChanges();

            var cars = new List <Inventory>
            {
                new Inventory {
                    Make = "VW", Color = "Black", PetName = "Zippy"
                },
                new Inventory {
                    Make = "Ford", Color = "Rust", PetName = "Rusty"
                },
                new Inventory {
                    Make = "Saab", Color = "Black", PetName = "Mel"
                },
                new Inventory {
                    Make = "BMW", Color = "Green", PetName = "Hank"
                }
            };

            context.Cars.AddRange(cars);
            context.SaveChanges();

            var orders = new List <Order>
            {
                new Order {
                    Car = cars[0], Customer = customers[0]
                },
                new Order {
                    Car = cars[1], Customer = customers[1]
                },
                new Order {
                    Car = cars[2], Customer = customers[2]
                },
                new Order {
                    Car = cars[3], Customer = customers[3]
                }
            };

            orders.ForEach(x => context.Add(x));
            context.SaveChanges();

            context.CreditRisks.Add(new CreditRisk
            {
                Id        = customers[4].Id,
                FirstName = customers[4].FirstName,
                LastName  = customers[4].LastName
            });

            context.Database.OpenConnection();
            try
            {
                //        var tName = context.GetTableName(typeof(CreditRisk));
                // We need dbcontext to access the models
                var models = context.Model;

                // Get all the entity types information
                var entityTypes = models.GetEntityTypes();

                // T is Name of class
                var entityTypeOfT = entityTypes.First(t => t.ClrType == typeof(CreditRisk));

                var tableNameAnnotation = entityTypeOfT.GetAnnotation("Relational:TableName");
                var tableName           = tableNameAnnotation.Value.ToString();

                var command = $"SET IDENTITY_INSERT dbo.{tableName} ON;";

                context.Database.ExecuteSqlCommand(command);
                context.SaveChanges();
                command = $"SET IDENTITY_INSERT dbo.CreditRisks OFF;";
                context.Database.ExecuteSqlCommand(command);
            }
            finally
            {
                context.Database.CloseConnection();
            }
        }
        public static void InitializeData(AutoLotContext context)
        {
            var customers = new List <Customer>
            {
                new Customer {
                    FirstName = "Dave", LastName = "Brenner"
                },
                new Customer {
                    FirstName = "Matt", LastName = "Walton"
                },
                new Customer {
                    FirstName = "Steve", LastName = "Hagen"
                },
                new Customer {
                    FirstName = "Pat", LastName = "Walton"
                },
                new Customer {
                    FirstName = "Bad", LastName = "Customer"
                }
            };

            context.Customers.AddRange(customers);
            context.SaveChanges();
            var cars = new List <Inventory>
            {
                new Inventory {
                    Make = "VW", Color = "Black", PetName = "Zippy"
                },
                new Inventory {
                    Make = "Ford", Color = "Rust", PetName = "Rusty"
                },
                new Inventory {
                    Make = "Saab", Color = "Black", PetName = "Mel"
                },
                new Inventory {
                    Make = "Yugo", Color = "Yellow", PetName = "Clunker"
                },
                new Inventory {
                    Make = "BMW", Color = "Black", PetName = "Bimmer"
                },
                new Inventory {
                    Make = "BMW", Color = "Green", PetName = "Hank"
                },
                new Inventory {
                    Make = "BMW", Color = "Pink", PetName = "Pinky"
                },
                new Inventory {
                    Make = "Pinto", Color = "Black", PetName = "Pete"
                },
                new Inventory {
                    Make = "Yugo", Color = "Brown", PetName = "Brownie"
                },
            };

            context.Cars.AddRange(cars);
            context.SaveChanges();
            var orders = new List <Order>
            {
                new Order {
                    Car = cars[0], Customer = customers[0]
                },
                new Order {
                    Car = cars[1], Customer = customers[1]
                },
                new Order {
                    Car = cars[2], Customer = customers[2]
                },
                new Order {
                    Car = cars[3], Customer = customers[3]
                },
            };

            context.Orders.AddRange(orders);
            context.SaveChanges();
        }