Esempio n. 1
0
    public void Insert_with_non_key_default_value()
    {
        using (var context = new BlogContextNonKeyDefaultValue(nameof(Insert_with_non_key_default_value)))
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            var blogs = new List <Blog>
            {
                new() { Name = "One Unicorn" },
                new()
                {
                    Name           = "Two Unicorns",
                    CreatedOn      = new DateTime(1969, 8, 3, 0, 10, 0),
                    NeedsConverter = new NeedsConverter(111),
                }
            };

            context.AddRange(blogs);

            context.SaveChanges();

            Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
            Assert.NotEqual(new DateTime(), blogs[1].CreatedOn);
            Assert.Equal(111, blogs[1].NeedsConverter.Value);
        }

        using (var context = new BlogContextNonKeyDefaultValue(nameof(Insert_with_non_key_default_value)))
        {
            var blogs = context.Blogs.OrderBy(e => e.Name).ToList();
            Assert.Equal(3, blogs.Count);

            Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
            Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);
            Assert.Equal(new DateTime(1974, 8, 3, 0, 10, 0), blogs[2].CreatedOn);

            blogs[0].CreatedOn = new DateTime(1973, 9, 3, 0, 10, 0);

            blogs[1].Name           = "X Unicorns";
            blogs[1].NeedsConverter = new NeedsConverter(222);

            blogs[2].Name           = "Y Unicorns";
            blogs[2].NeedsConverter = new NeedsConverter(333);

            context.SaveChanges();
        }

        using (var context = new BlogContextNonKeyDefaultValue(nameof(Insert_with_non_key_default_value)))
        {
            var blogs = context.Blogs.OrderBy(e => e.Name).ToList();
            Assert.Equal(3, blogs.Count);

            Assert.Equal(new DateTime(1973, 9, 3, 0, 10, 0), blogs[0].CreatedOn);
            Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);
            Assert.Equal(222, blogs[1].NeedsConverter.Value);
            Assert.Equal(new DateTime(1974, 8, 3, 0, 10, 0), blogs[2].CreatedOn);
            Assert.Equal(333, blogs[2].NeedsConverter.Value);
        }
    }
        public void Insert_with_non_key_default_value()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextNonKeyDefaultValue(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    var blogs = new List <Blog>
                    {
                        new Blog
                        {
                            Name = "One Unicorn"
                        },
                        new Blog
                        {
                            Name      = "Two Unicorns",
                            CreatedOn = new DateTime(1969, 8, 3, 0, 10, 0)
                        }
                    };

                    context.AddRange(blogs);

                    context.SaveChanges();

                    Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
                    Assert.NotEqual(new DateTime(), blogs[1].CreatedOn);
                    Assert.Null(blogs[0].OtherId);
                    Assert.Null(blogs[1].OtherId);
                }

                using (var context = new BlogContextNonKeyDefaultValue(testStore.Name))
                {
                    var blogs = context.Blogs.OrderBy(e => e.Name).ToList();

                    Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
                    Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);

                    blogs[0].CreatedOn = new DateTime(1973, 9, 3, 0, 10, 0);
                    blogs[1].Name      = "Zwo Unicorns";

                    context.SaveChanges();
                }

                using (var context = new BlogContextNonKeyDefaultValue(testStore.Name))
                {
                    var blogs = context.Blogs.OrderBy(e => e.Name).ToList();

                    Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);
                    Assert.Equal(new DateTime(1973, 9, 3, 0, 10, 0), blogs[0].CreatedOn);
                }
            }
        }
        public void Insert_with_non_key_default_value()
        {
            using var testStore = SqlServerTestStore.CreateInitialized(DatabaseName);

            using (var context = new BlogContextNonKeyDefaultValue(testStore.Name))
            {
                context.Database.EnsureCreatedResiliently();

                var blogs = new List <Blog>
                {
                    new() { Name = "One Unicorn" },
                    new()
                    {
                        Name               = "Two Unicorns",
                        CreatedOn          = new DateTime(1969, 8, 3, 0, 10, 0),
                        NeedsConverter     = new NeedsConverter(111),
                        GeometryCollection = GeometryFactory.CreateGeometryCollection(
                            new Geometry[] { GeometryFactory.CreatePoint(new Coordinate(1, 3)) })
                    }
                };

                context.AddRange(blogs);

                context.SaveChanges();

                Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
                Assert.NotEqual(new DateTime(), blogs[1].CreatedOn);
                Assert.Equal(111, blogs[1].NeedsConverter.Value);

                var point = ((Point)blogs[1].GeometryCollection.Geometries[0]);
                Assert.Equal(1, point.X);
                Assert.Equal(3, point.Y);
            }

            using (var context = new BlogContextNonKeyDefaultValue(testStore.Name))
            {
                var blogs = context.Blogs.OrderBy(e => e.Name).ToList();
                Assert.Equal(3, blogs.Count);

                Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
                Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);
                Assert.Equal(new DateTime(1974, 8, 3, 0, 10, 0), blogs[2].CreatedOn);

                var point1 = ((Point)blogs[1].GeometryCollection.Geometries[0]);
                Assert.Equal(1, point1.X);
                Assert.Equal(3, point1.Y);

                var point2 = ((Point)blogs[2].GeometryCollection.Geometries[0]);
                Assert.Equal(1, point2.X);
                Assert.Equal(2, point2.Y);

                blogs[0].CreatedOn = new DateTime(1973, 9, 3, 0, 10, 0);

                blogs[1].Name           = "X Unicorns";
                blogs[1].NeedsConverter = new NeedsConverter(222);
                blogs[1].GeometryCollection.Geometries[0] = GeometryFactory.CreatePoint(new Coordinate(1, 11));

                blogs[2].Name           = "Y Unicorns";
                blogs[2].NeedsConverter = new NeedsConverter(333);
                blogs[2].GeometryCollection.Geometries[0] = GeometryFactory.CreatePoint(new Coordinate(1, 22));

                context.SaveChanges();
            }

            using (var context = new BlogContextNonKeyDefaultValue(testStore.Name))
            {
                var blogs = context.Blogs.OrderBy(e => e.Name).ToList();
                Assert.Equal(3, blogs.Count);

                Assert.Equal(new DateTime(1973, 9, 3, 0, 10, 0), blogs[0].CreatedOn);
                Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);
                Assert.Equal(222, blogs[1].NeedsConverter.Value);
                Assert.Equal(new DateTime(1974, 8, 3, 0, 10, 0), blogs[2].CreatedOn);
                Assert.Equal(333, blogs[2].NeedsConverter.Value);

                var point1 = ((Point)blogs[1].GeometryCollection.Geometries[0]);
                Assert.Equal(1, point1.X);
                Assert.Equal(11, point1.Y);

                var point2 = ((Point)blogs[2].GeometryCollection.Geometries[0]);
                Assert.Equal(1, point2.X);
                Assert.Equal(22, point2.Y);
            }
        }