public void BeginnenMetEenContext() { using (var context = new SeriesContext(options)) { context.Database.Migrate(); var serie = new Serie { Id = 0, Title = $"Narcos{Guid.NewGuid()}" }; context.Series.Add(serie); var season = new Season { Id = 0, Title = "Season 1" }; serie.Seasons.Add(season); var episode = new Episode { Id = 0, Title = "Descenso" }; season.Episodes.Add(episode); context.SaveChanges(); } }
public void DeTitleVanEenSerieMoetUniekZijn() { using (var connection = new SqlConnection(@"Server=.\SQLEXPRESS;Database=Series;Trusted_Connection=true")) { connection.Open(); using (var tx = connection.BeginTransaction()) { var options = new DbContextOptionsBuilder <SeriesContext>() .UseSqlServer(connection) .Options; // Arrange using (var context = new SeriesContext(options)) { context.Database.UseTransaction(tx); context.Series.Add(new Serie { Title = "UNIEK" }); context.SaveChanges(); } using (var context = new SeriesContext(options)) { context.Database.UseTransaction(tx); context.Series.Add(new Serie { Title = "UNIEK" }); var ex = Assert.Throws <DbUpdateException>(() => context.SaveChanges()); Assert.Contains("duplicate", ex.InnerException.Message); Assert.Contains("UNIEK", ex.InnerException.Message); } } } }
public void HoeWerkenTransactionsEnUniqueConstraintsTegenSqlite() { var file = "test.db"; File.Delete(file); using (var connection = new SqliteConnection($"FileName={file}")) { connection.Open(); var options = new DbContextOptionsBuilder <SeriesContext>() .UseSqlite(connection) .Options; using (var context = new SeriesContext(options)) { context.Database.EnsureCreated(); } using (var tx = connection.BeginTransaction()) { // Arrange using (var context = new SeriesContext(options)) { context.Database.UseTransaction(tx); context.Series.Add(new Serie { Title = "UNIEK" }); context.SaveChanges(); } using (var context = new SeriesContext(options)) { context.Database.UseTransaction(tx); context.Series.Add(new Serie { Title = "UNIEK" }); var ex = Assert.Throws <DbUpdateException>(() => context.SaveChanges()); Assert.Contains("UNIQUE", ex.InnerException.Message); } } } }