static void Main(string[] args) { GringottsContext ctx = new GringottsContext(); ctx.Database.Delete(); ctx.Database.Create(); var wizzard = new WizzardDeposits() { FirstName = "Bate", LastName = "Pesho", MagicWandSize = int.MaxValue - 10000, Age = 20 }; var tran = ctx.Database.BeginTransaction(); try { ctx.WizzardDeposits.Add(wizzard); ctx.SaveChanges(); tran.Commit(); System.Console.WriteLine("Bate Pesho added."); } catch (System.Exception) { tran.Rollback(); System.Console.WriteLine("Neshto grumna."); } }
static void Main(string[] args) { var ctx = new GringottsContext(); ctx.Database.Initialize(true); Models.WizardDeposits dumbledore = new Models.WizardDeposits() { FirstName = "Albus", LastName = "Dumbledore", Age = 150, MagicWandCreator = "Antioch Peverell", MagicWandSize = 15, DepositStartDate = new DateTime(2016, 10, 20), DepositExpirationDate = new DateTime(2020, 10, 20), DepositAmount = 20000.24m, DepositCharge = 0.2m, IsDepositExpired = false, }; ctx.Deposits.Add(dumbledore); try { ctx.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (var entityvalidationError in ex.EntityValidationErrors) { foreach (var validatioError in entityvalidationError .ValidationErrors) { System.Diagnostics.Debug.Write("Property: " + validatioError.PropertyName + "Error: " + validatioError .ErrorMessage); } } } Console.WriteLine("Added WizardDeposits:"); ctx.Deposits.Select(d => d.LastName).ToList() .ForEach(Console.WriteLine); }
private static void Main(string[] args) { //The migrations were not needed for this exercises. I just tried different things with them; var context = new GringottsContext(); WizzardDeposit dumbledoreDeposit = new WizzardDeposit() { FirstName = "Albus", LastName = "Dubledore", Age = 15, MagicWandCreator = "Antioch Peverell", MagicWandSize = 15, DepositStartDate = new DateTime(2017, 03, 01), DepositExpirationDate = new DateTime(2020, 03, 01), DepositAmount = 20000.24m, DepositCharge = 0.2, IsDepositExpired = false }; context.WizzardDeposits.Add(dumbledoreDeposit); try { context.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (var ev in ex.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", ev.Entry.Entity.GetType().Name, ev.Entry.State); foreach (var ve in ev.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }