예제 #1
0
        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);
                    }
                }
            }
        }
예제 #2
0
        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();
            }
        }
예제 #3
0
        public void HoeNeemIkSeizoenenMeeBijHetOpvragenVanEenSerie()
        {
            using (var context = new SeriesContext(options))
            {
                var serie = from s in context
                            .Series
                            .Include(s => s.Seasons)     // dit is nodig om ook de seasons erbij te laden
                            .ThenInclude(s => s.Episodes)
                            where s.Seasons.Any() && s.Seasons.SelectMany(season => season.Episodes).Any()
                            select s;

                Assert.True(serie.First().Seasons.Any());
                Assert.True(serie.First().Seasons.First().Episodes.Any());
            }
        }
예제 #4
0
        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);
                    }
                }
            }
        }