private static void RemoveTwoEntitiesWithFilteredIndex(JustDatabaseContext context) { Console.WriteLine("Deleting entities..."); var entitiesToDelete = context.Users.Where(x => x.IsActive == false); context.RemoveRange(entitiesToDelete); SaveChangesAndWriteToConsoleException(context); }
private static void CreateTwoTheSameEntitiesWhichDoNotMetFilterConditions(JustDatabaseContext context) { context.Users.Add(new Model.User { IsActive = false, Login = "******", Email = "*****@*****.**" }); context.Users.Add(new Model.User { IsActive = false, Login = "******", Email = "*****@*****.**" }); context.SaveChanges(); }
static void Main(string[] args) { InitializeDiContainer(); JustDatabaseContext context = MigrateDatabase(); CreateTwoTheSameEntitiesWhichDoNotMetFilterConditions(context); UpdateTwoEntitiesWithFilteredIndex(context); RemoveTwoEntitiesWithFilteredIndex(context); // CleanupDatabase(context); Console.ReadKey(); }
private static void UpdateTwoEntitiesWithFilteredIndex(JustDatabaseContext context) { Console.WriteLine("Updating entities..."); var entitiesToUpdate = context.Users.Where(x => x.IsActive == false); int i = 0; foreach (var entity in entitiesToUpdate) { entity.Login = "******" + i++; } SaveChangesAndWriteToConsoleException(context); }
private static void SaveChangesAndWriteToConsoleException(JustDatabaseContext context) { try { // The problem occurs during saving changes context.SaveChanges(); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ex.Message); Console.ResetColor(); } }
private static void CleanupDatabase(JustDatabaseContext context) { context.RemoveRange(context.Users); context.SaveChanges(); }