public async Task Only_one_part_of_a_composite_key_needs_to_vary_for_uniquness()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFramework()
                                  .AddSqlServer()
                                  .ServiceCollection
                                  .BuildServiceProvider();

            var ids = new int[3];

            using (var context = new BronieContext(serviceProvider, "CompositeEarthPonies"))
            {
                context.Database.EnsureCreated();

                var pony1 = await context.AddAsync(new EarthPony { Id2 = 7, Name = "Apple Jack 1" });

                var pony2 = await context.AddAsync(new EarthPony { Id2 = 7, Name = "Apple Jack 2" });

                var pony3 = await context.AddAsync(new EarthPony { Id2 = 7, Name = "Apple Jack 3" });

                await context.SaveChangesAsync();

                ids[0] = pony1.Id1;
                ids[1] = pony2.Id1;
                ids[2] = pony3.Id1;
            }

            using (var context = new BronieContext(serviceProvider, "CompositeEarthPonies"))
            {
                var ponies = context.EarthPonies.ToList();
                Assert.Equal(ponies.Count, ponies.Count(e => e.Name == "Apple Jack 1") * 3);

                Assert.Equal("Apple Jack 1", ponies.Single(e => e.Id1 == ids[0]).Name);
                Assert.Equal("Apple Jack 2", ponies.Single(e => e.Id1 == ids[1]).Name);
                Assert.Equal("Apple Jack 3", ponies.Single(e => e.Id1 == ids[2]).Name);

                ponies.Single(e => e.Id1 == ids[1]).Name = "Pinky Pie 2";

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositeEarthPonies"))
            {
                var ponies = context.EarthPonies.ToList();
                Assert.Equal(ponies.Count, ponies.Count(e => e.Name == "Apple Jack 1") * 3);

                Assert.Equal("Apple Jack 1", ponies.Single(e => e.Id1 == ids[0]).Name);
                Assert.Equal("Pinky Pie 2", ponies.Single(e => e.Id1 == ids[1]).Name);
                Assert.Equal("Apple Jack 3", ponies.Single(e => e.Id1 == ids[2]).Name);

                context.EarthPonies.RemoveRange(ponies);

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositeEarthPonies"))
            {
                Assert.Equal(0, context.EarthPonies.Count());
            }
        }
        private static async Task AddEntitiesAsync(IServiceProvider serviceProvider)
        {
            using (var context = new BronieContext(serviceProvider))
            {
                for (var i = 0; i < 50; i++)
                {
                    await context.AddAsync(new Pegasus { Name = "Rainbow Dash " + i });
                    await context.AddAsync(new Pegasus { Name = "Fluttershy " + i });
                }

                await context.SaveChangesAsync();
            }
        }
示例#3
0
        private static async Task AddEntitiesAsync(IServiceProvider serviceProvider, string databaseName)
        {
            using (var context = new BronieContext(serviceProvider, databaseName))
            {
                for (var i = 0; i < 10; i++)
                {
                    await context.AddAsync(new Pegasus { Name = "Rainbow Dash " + i });

                    await context.AddAsync(new Pegasus { Name = "Fluttershy " + i });
                }

                await context.SaveChangesAsync();
            }
        }
示例#4
0
        public async Task Can_use_sequential_GUID_end_to_end_async()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFramework()
                                  .AddSqlServer()
                                  .UseLoggerFactory <LoggerFactory>()
                                  .ServiceCollection
                                  .BuildServiceProvider();

            using (var context = new BronieContext(serviceProvider, "GooieBronies"))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                for (var i = 0; i < 50; i++)
                {
                    await context.AddAsync(new Pegasus { Name = "Rainbow Dash " + i });
                }

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "GooieBronies"))
            {
                var pegasuses = await context.Pegasuses.OrderBy(e => e.Id).ToListAsync();

                for (var i = 0; i < 50; i++)
                {
                    Assert.Equal("Rainbow Dash " + i, pegasuses[i].Name);
                }
            }
        }
        public async Task Can_use_GUIDs_end_to_end_async()
        {
            var serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddInMemoryStore()
                .ServiceCollection
                .BuildServiceProvider();

            var guids = new List<Guid>();
            var guidsHash = new HashSet<Guid>();
            using (var context = new BronieContext(serviceProvider))
            {
                for (var i = 0; i < 10; i++)
                {
                    guids.Add((await context.AddAsync(new Pegasus { Name = "Rainbow Dash " + i })).Id);
                    guidsHash.Add(guids.Last());
                }

                await context.SaveChangesAsync();
            }

            Assert.Equal(10, guidsHash.Count);

            using (var context = new BronieContext(serviceProvider))
            {
                var pegasuses = await context.Pegasuses.OrderBy(e => e.Name).ToListAsync();

                for (var i = 0; i < 10; i++)
                {
                    Assert.Equal(guids[i], pegasuses[i].Id);
                }
            }
        }
示例#6
0
        public async Task Can_use_sequential_GUID_end_to_end_async()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFramework()
                                  .AddSqlServer()
                                  .ServiceCollection
                                  .BuildServiceProvider();

            using (var context = new BronieContext(serviceProvider, "GooieBronies"))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                for (var i = 0; i < 50; i++)
                {
                    await context.AddAsync(new Pegasus { Name = "Rainbow Dash " + i });
                }

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "GooieBronies"))
            {
                // TODO: Currently test passes because SQL Server happens to return values in
                // inserted order. When OrderBy is done on server it should be added to the query.
                //var pegasuses = await context.Pegasuses.OrderBy(e => e.Id).ToListAsync();
                var pegasuses = await context.Pegasuses.ToListAsync();

                for (var i = 0; i < 50; i++)
                {
                    Assert.Equal("Rainbow Dash " + i, pegasuses[i].Name);
                }
            }
        }
        public async Task Can_use_sequential_GUID_end_to_end_async()
        {
            var serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddSqlServer()
                .ServiceCollection
                .BuildServiceProvider();

            using (var context = new BronieContext(serviceProvider, "GooieBronies"))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                for (var i = 0; i < 50; i++)
                {
                    await context.AddAsync(new Pegasus { Name = "Rainbow Dash " + i });
                }

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "GooieBronies"))
            {
                // TODO: Currently test passes because SQL Server happens to return values in
                // inserted order. When OrderBy is done on server it should be added to the query.
                //var pegasuses = await context.Pegasuses.OrderBy(e => e.Id).ToListAsync();
                var pegasuses = await context.Pegasuses.ToListAsync();

                for (var i = 0; i < 50; i++)
                {
                    Assert.Equal("Rainbow Dash " + i, pegasuses[i].Name);
                }
            }
        }
示例#8
0
        public async Task Can_use_generated_values_in_composite_key_end_to_end()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFramework()
                                  .AddSqlServer()
                                  .UseLoggerFactory <LoggerFactory>()
                                  .ServiceCollection
                                  .BuildServiceProvider();

            long id1;
            var  id2 = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
            Guid id3;

            using (var context = new BronieContext(serviceProvider, "CompositeUnicorns"))
            {
                context.Database.EnsureCreated();

                var added = await context.AddAsync(new Unicorn { Id2 = id2, Name = "Rarity" });

                Assert.True(added.Id1 < 0);
                Assert.NotEqual(Guid.Empty, added.Id3);

                await context.SaveChangesAsync();

                Assert.True(added.Id1 > 0);

                id1 = added.Id1;
                id3 = added.Id3;
            }

            using (var context = new BronieContext(serviceProvider, "CompositeUnicorns"))
            {
                Assert.Equal(1, context.Unicorns.Count(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3));
            }

            using (var context = new BronieContext(serviceProvider, "CompositeUnicorns"))
            {
                var unicorn = context.Unicorns.Single(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3);

                unicorn.Name = "Bad Hair Day";

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositeUnicorns"))
            {
                var unicorn = context.Unicorns.Single(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3);

                Assert.Equal("Bad Hair Day", unicorn.Name);

                context.Unicorns.Remove(unicorn);

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositeUnicorns"))
            {
                Assert.Equal(0, context.Unicorns.Count(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3));
            }
        }
        public async Task Can_use_generated_values_in_composite_key_end_to_end()
        {
            var serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddSqlServer()
                .ServiceCollection
                .BuildServiceProvider();

            long id1;
            var id2 = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
            Guid id3;

            using (var context = new BronieContext(serviceProvider, "CompositeUnicorns"))
            {
                context.Database.EnsureCreated();

                var added = await context.AddAsync(new Unicorn { Id2 = id2, Name = "Rarity" });

                Assert.True(added.Id1 < 0);
                Assert.NotEqual(Guid.Empty, added.Id3);

                await context.SaveChangesAsync();

                Assert.True(added.Id1 > 0);

                id1 = added.Id1;
                id3 = added.Id3;
            }

            using (var context = new BronieContext(serviceProvider, "CompositeUnicorns"))
            {
                Assert.Equal(1, context.Unicorns.Count(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3));
            }

            using (var context = new BronieContext(serviceProvider, "CompositeUnicorns"))
            {
                var unicorn = context.Unicorns.Single(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3);

                unicorn.Name = "Bad Hair Day";

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositeUnicorns"))
            {
                var unicorn = context.Unicorns.Single(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3);

                Assert.Equal("Bad Hair Day", unicorn.Name);

                context.Unicorns.Remove(unicorn);

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositeUnicorns"))
            {
                Assert.Equal(0, context.Unicorns.Count(e => e.Id1 == id1 && e.Id2 == id2 && e.Id3 == id3));
            }
        }
示例#10
0
        public async Task Can_use_two_non_generated_integers_as_composite_key_end_to_end()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFramework()
                                  .AddSqlServer()
                                  .UseLoggerFactory <LoggerFactory>()
                                  .ServiceCollection
                                  .BuildServiceProvider();

            var ticks = DateTime.UtcNow.Ticks;

            using (var context = new BronieContext(serviceProvider, "CompositePegasuses"))
            {
                context.Database.EnsureCreated();

                await context.AddAsync(new Pegasus { Id1 = ticks, Id2 = ticks + 1, Name = "Rainbow Dash" });

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositePegasuses"))
            {
                var pegasus = context.Pegasuses.Single(e => e.Id1 == ticks && e.Id2 == ticks + 1);

                pegasus.Name = "Rainbow Crash";

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositePegasuses"))
            {
                var pegasus = context.Pegasuses.Single(e => e.Id1 == ticks && e.Id2 == ticks + 1);

                Assert.Equal("Rainbow Crash", pegasus.Name);

                context.Pegasuses.Remove(pegasus);

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositePegasuses"))
            {
                Assert.Equal(0, context.Pegasuses.Count(e => e.Id1 == ticks && e.Id2 == ticks + 1));
            }
        }
        public async Task Can_use_two_non_generated_integers_as_composite_key_end_to_end()
        {
            var serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddSqlServer()
                .ServiceCollection
                .BuildServiceProvider();

            var ticks = DateTime.UtcNow.Ticks;

            using (var context = new BronieContext(serviceProvider, "CompositePegasuses"))
            {
                context.Database.EnsureCreated();

                await context.AddAsync(new Pegasus { Id1 = ticks, Id2 = ticks + 1, Name = "Rainbow Dash" });
                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositePegasuses"))
            {
                var pegasus = context.Pegasuses.Single(e => e.Id1 == ticks && e.Id2 == ticks + 1);

                pegasus.Name = "Rainbow Crash";

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositePegasuses"))
            {
                var pegasus = context.Pegasuses.Single(e => e.Id1 == ticks && e.Id2 == ticks + 1);

                Assert.Equal("Rainbow Crash", pegasus.Name);

                context.Pegasuses.Remove(pegasus);

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "CompositePegasuses"))
            {
                Assert.Equal(0, context.Pegasuses.Count(e => e.Id1 == ticks && e.Id2 == ticks + 1));
            }
        }
示例#12
0
        public async Task Can_use_explicit_values()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFramework()
                                  .AddSqlServer()
                                  .UseLoggerFactory <LoggerFactory>()
                                  .ServiceCollection
                                  .BuildServiceProvider();

            var guids = new List <Guid>();

            using (var context = new BronieContext(serviceProvider, "GooieExplicitBronies"))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                for (var i = 0; i < 50; i++)
                {
                    guids.Add((await context.AddAsync(new Pegasus {
                        Name = "Rainbow Dash " + i, Index = i, Id = Guid.NewGuid()
                    })).Id);
                }

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider, "GooieExplicitBronies"))
            {
                var pegasuses = await context.Pegasuses.OrderBy(e => e.Index).ToListAsync();

                for (var i = 0; i < 50; i++)
                {
                    Assert.Equal("Rainbow Dash " + i, pegasuses[i].Name);
                    Assert.Equal(guids[i], pegasuses[i].Id);
                }
            }
        }
        public async Task Can_use_GUIDs_end_to_end_async()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFramework()
                                  .AddInMemoryStore()
                                  .UseLoggerFactory <LoggerFactory>()
                                  .ServiceCollection
                                  .BuildServiceProvider();

            var guids     = new List <Guid>();
            var guidsHash = new HashSet <Guid>();

            using (var context = new BronieContext(serviceProvider))
            {
                for (var i = 0; i < 10; i++)
                {
                    guids.Add((await context.AddAsync(new Pegasus {
                        Name = "Rainbow Dash " + i
                    })).Id);
                    guidsHash.Add(guids.Last());
                }

                await context.SaveChangesAsync();
            }

            Assert.Equal(10, guidsHash.Count);

            using (var context = new BronieContext(serviceProvider))
            {
                var pegasuses = await context.Pegasuses.OrderBy(e => e.Name).ToListAsync();

                for (var i = 0; i < 10; i++)
                {
                    Assert.Equal(guids[i], pegasuses[i].Id);
                }
            }
        }
        public async Task Only_one_part_of_a_composite_key_needs_to_vary_for_uniquness()
        {
            var serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddInMemoryStore()
                .ServiceCollection
                .BuildServiceProvider();

            var ids = new int[3];

            using (var context = new BronieContext(serviceProvider))
            {
                var pony1 = await context.AddAsync(new EarthPony { Id2 = 7, Name = "Apple Jack 1" });
                var pony2 = await context.AddAsync(new EarthPony { Id2 = 7, Name = "Apple Jack 2" });
                var pony3 = await context.AddAsync(new EarthPony { Id2 = 7, Name = "Apple Jack 3" });

                await context.SaveChangesAsync();

                ids[0] = pony1.Id1;
                ids[1] = pony2.Id1;
                ids[2] = pony3.Id1;
            }

            using (var context = new BronieContext(serviceProvider))
            {
                var ponies = context.EarthPonies.ToList();
                Assert.Equal(ponies.Count, ponies.Count(e => e.Name == "Apple Jack 1") * 3);

                Assert.Equal("Apple Jack 1", ponies.Single(e => e.Id1 == ids[0]).Name);
                Assert.Equal("Apple Jack 2", ponies.Single(e => e.Id1 == ids[1]).Name);
                Assert.Equal("Apple Jack 3", ponies.Single(e => e.Id1 == ids[2]).Name);

                ponies.Single(e => e.Id1 == ids[1]).Name = "Pinky Pie 2";

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider))
            {
                var ponies = context.EarthPonies.ToList();
                Assert.Equal(ponies.Count, ponies.Count(e => e.Name == "Apple Jack 1") * 3);

                Assert.Equal("Apple Jack 1", ponies.Single(e => e.Id1 == ids[0]).Name);
                Assert.Equal("Pinky Pie 2", ponies.Single(e => e.Id1 == ids[1]).Name);
                Assert.Equal("Apple Jack 3", ponies.Single(e => e.Id1 == ids[2]).Name);

                context.EarthPonies.RemoveRange(ponies);

                await context.SaveChangesAsync();
            }

            using (var context = new BronieContext(serviceProvider))
            {
                Assert.Equal(0, context.EarthPonies.Count());
            }
        }