예제 #1
0
        public void CanInsertSamuraiWithSaveChanges()
        {
            // SQL Server
            var optionsBuilderSQLServer = new DbContextOptionsBuilder();

            InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(optionsBuilderSQLServer, "SamuraiSQLData");
            using (var context = new SQLServer.Data.SamuraiContext(optionsBuilderSQLServer.Options))
            {
                var samurai = new Samurai {
                    Name = "Julie"
                };
                context.Samurais.Add(samurai);
                context.SaveChanges();
            }
            using (var context = new SQLServer.Data.SamuraiContext(optionsBuilderSQLServer.Options))
            {
                Assert.Equal(1, context.Samurais.Count());
            }

            // Postgre SQL
            var optionsBuilderPostgreSQL = new DbContextOptionsBuilder();

            InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(optionsBuilderPostgreSQL, "SamuraiPostgreSQL");
            using (var context = new PostgreSQL.Data.SamuraiContext(optionsBuilderPostgreSQL.Options))
            {
                var samurai = new Samurai {
                    Name = "Julie"
                };
                context.Samurais.Add(samurai);
                context.SaveChanges();
            }
            using (var context = new PostgreSQL.Data.SamuraiContext(optionsBuilderPostgreSQL.Options))
            {
                Assert.Equal(1, context.Samurais.Count());
            }


            // SQLite
            var optionsBuilderSQLite = new DbContextOptionsBuilder();

            InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(optionsBuilderSQLite, "SamuraiSQLite");
            using (var context = new SQLite.Data.SamuraiContext(optionsBuilderSQLite.Options))
            {
                var samurai = new Samurai {
                    Name = "Julie"
                };
                context.Samurais.Add(samurai);
                context.SaveChanges();
            }
            using (var context = new SQLite.Data.SamuraiContext(optionsBuilderSQLite.Options))
            {
                Assert.Equal(1, context.Samurais.Count());
            }
        }
예제 #2
0
        public void CanInsertSamuraiIntoSQLServerDatabase()
        {
            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=TestDb;Integrated Security=True");
            using (var context = new SQLServer.Data.SamuraiContext(optionsBuilder.Options))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
                var samurai = new Samurai {
                    Name = "Jaiprakash"
                };
                this.output.WriteLine($"Default samurai id: {samurai.Id}");
                context.Samurais.Add(samurai);
                var efDefaultId = samurai.Id;
                this.output.WriteLine($"EF default samurai id: {efDefaultId}");
                context.SaveChanges();
                this.output.WriteLine($"DB assigned samurai id: {samurai.Id}");
                Assert.NotEqual(efDefaultId, samurai.Id);
            }
        }