static void Main(string[] args) { Console.WriteLine("***** Fun with ADO.NET EF Core 2 *****\n"); using (var context = new AutoLotContext()) { MyDataInitializer.RecreateDatabase(context); MyDataInitializer.InitializeData(context); foreach (inventory c in context.Cars) { Console.WriteLine(c); } } Console.WriteLine("***** Using a Repository *****\n"); using (var context = new AutoLotContext()) { using var repo = new InventoryRepo(context); foreach (inventory c in repo.GetAll()) { Console.WriteLine(c); } } TestConcurrency(); Console.ReadLine(); }
private static void ExecuteDeleteSql(AutoLotContext context, string tableName) { //With 2.0, must separate string interpolation if not passing in params var rawSqlString = $"Delete from dbo.{tableName}"; context.Database.ExecuteSqlCommand(rawSqlString); }
public static void ClearData(AutoLotContext context) { ExecuteDeleteSql(context, "Orders"); ExecuteDeleteSql(context, "Customers"); ExecuteDeleteSql(context, "Inventory"); ResetIdentity(context); }
static void Main(string[] args) { Console.WriteLine("EF Core 2"); using (var context = new AutoLotContext()) { MyDataInitializer.RecreateDatabase(context); MyDataInitializer.InitializeData(context); foreach (var car in context.Cars) { Console.WriteLine(car); } } Console.WriteLine("Using Repository"); using (var repo = new InventoryRepo()) { foreach (var car in repo.GetAll()) { Console.WriteLine("Repo: " + car); } } Console.ReadLine(); using (var context = new AutoLotContext()) { var car = context.Cars.Include(x => x.Orders).ThenInclude(x => x.Customer).FirstOrDefault(x => x.Id == 4); Console.WriteLine($"The customer of {car} is:"); Console.WriteLine(car.Orders.FirstOrDefault().Customer); } }
private static void RemoveRecordByCar(inventory carToDelete) { using (var context = new AutoLotContext()) { using var repo = new InventoryRepo(context); repo.Delete(carToDelete); } }
private static void RemoveRecordById(int carId, byte[] timeStamp) { using (var context = new AutoLotContext()) { using var repo = new InventoryRepo(context); repo.Delete(carId, timeStamp); } }
public static void ResetIdentity(AutoLotContext context) { var tables = new[] { "Inventory", "Orders", "Customers" }; foreach (var itm in tables) { context.Database.ExecuteSqlInterpolated($"DBCC CHECKIDENT (\"dbo.{itm}\", RESEED, -1);"); } }
public static void ResetIdentity(AutoLotContext context) { var tables = new[] { "Inventory", "Orders", "Customers", "CreditRisks" }; foreach (var itm in tables) { var rawSqlString = $"DBCC CHECKIDENT (\"dbo.{itm}\", RESEED, -1);"; context.Database.ExecuteSqlCommand(rawSqlString); } }
private static void ResetIdentity(AutoLotContext context) { var tables = new[] { "Inventory", "Orders", "Customers", "CreditRisks" }; foreach (var itm in tables) { //With 2.0, must separate string interpolation if not passing in params var rawSqlString = $"DBCC CHECKIDENT (\"dbo.{itm}\", RESEED, -1);"; context.Database.ExecuteSqlCommand(rawSqlString); } }
static void Main(string[] args) { using (var context = new AutoLotContext()) { MyDataInitializer.RecreateDatabase(context); MyDataInitializer.InitializeData(context); foreach (Inventory c in context.Cars) { Console.WriteLine(c); } using (var repo = new InventoryRepo(context)) { foreach (Inventory c in repo.GetAll()) { Console.WriteLine(c); } } } Console.ReadLine(); }
static void Main() { Console.WriteLine("FUN with ADO.NET EF Core 2\n"); using (var context = new AutoLotContext()) { MyDataInitializer.RecreateDatabase(context); MyDataInitializer.InitializeData(context); foreach (Inventory c in context.Cars) { Console.WriteLine(c); } } Console.WriteLine("Using a Repository\n"); using (var repo = new InventoryRepo()) { foreach (Inventory c in repo.GetAll()) { Console.WriteLine(c); } } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Function with ADO.NET EF Core 2 ****\n"); using (var context = new AutoLotContext()) { MyDataInitializer.RecreateDatabase(context); MyDataInitializer.InitializeData(context); foreach (Inventory c in context.Cars) { Console.WriteLine(c); } //Dependency Injection Console.WriteLine("Using a Repository\n"); using (var repo = new InventoryRepo(context)) { foreach (Inventory c in repo.GetAll()) { Console.WriteLine(c); } } //Console.ReadLine(); } //fallback Console.WriteLine("Using a Repository\n"); using (var repo = new InventoryRepo()) { foreach (Inventory c in repo.GetAll()) { Console.WriteLine(c); } } Console.ReadLine(); }
public static void ExecuteDeleteSql(AutoLotContext context, string tableName) { var rawSqlString = $"Delete from dbo.{tableName}"; context.Database.ExecuteSqlCommand(rawSqlString); }
public static void RecreateDatabase(AutoLotContext context) { context.Database.EnsureDeleted(); context.Database.Migrate(); }
public static void ExecuteDeleteSql(AutoLotContext context, string tableName) { string command = $"Delete from dbo.{tableName}"; context.Database.ExecuteSqlCommand(command); }
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(); }
public BaseRepo(AutoLotContext context) { this.db = context; this.table = this.db.Set <T>(); }
public OrdersController(AutoLotContext context) { _context = context; }
public static void ExecuteDeleteSql(AutoLotContext context, string tableName) { context.Database.ExecuteSqlInterpolated($"Delete from dbo.{tableName}"); }
public InventoryController(AutoLotContext context) { _context = context; }
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 InventoryRepo(AutoLotContext context) : base(context) { }
public CustomersRepo(AutoLotContext context) : base(context) { }
public BaseRepo(AutoLotContext context) { _db = context; _table = _db.Set <T>(); }
public CustomersController(AutoLotContext context) { _context = context; }