public void Insert_string_to_Identity_column_using_value_converter()
    {
        using var testStore = SqlServerTestStore.CreateInitialized(DatabaseName);
        using (var context = new BlogContextStringToIdentityUsingValueConverter(testStore.Name))
        {
            context.Database.EnsureCreatedResiliently();

            context.AddRange(
                new BlogWithStringKey {
                Name = "One Unicorn"
            }, new BlogWithStringKey {
                Name = "Two Unicorns"
            });

            context.SaveChanges();
        }

        using (var context = new BlogContextStringToIdentityUsingValueConverter(testStore.Name))
        {
            var blogs = context.StringyBlogs.OrderBy(e => e.Id).ToList();

            Assert.Equal("1", blogs[0].Id);
            Assert.Equal("2", blogs[1].Id);
        }
    }
Beispiel #2
0
        public void Insert_with_key_default_value_from_sequence()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextKeyColumnWithDefaultValue(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    context.AddRange(new Blog {
                        Name = "One Unicorn"
                    }, new Blog {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

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

                    Assert.Equal(77, blogs[0].Id);
                    Assert.Equal(78, blogs[1].Id);
                }
            }
        }
Beispiel #3
0
        public void Insert_with_explicit_non_default_keys()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextNoKeyGeneration(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    context.AddRange(new Blog {
                        Id = 66, Name = "One Unicorn"
                    }, new Blog {
                        Id = 67, Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

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

                    Assert.Equal(66, blogs[0].Id);
                    Assert.Equal(67, blogs[1].Id);
                }
            }
        }
Beispiel #4
0
        public static async Task Deletes_database(bool async, bool ambientTransaction)
        {
            using (var testDatabase = SqlServerTestStore.CreateInitialized("DeleteBlogging"))
            {
                testDatabase.CloseConnection();

                var creator = GetDatabaseCreator(testDatabase);

                Assert.True(async ? await creator.ExistsAsync() : creator.Exists());

                using (CreateTransactionScope(ambientTransaction))
                {
                    if (async)
                    {
                        await creator.DeleteAsync();
                    }
                    else
                    {
                        creator.Delete();
                    }
                }

                Assert.False(async ? await creator.ExistsAsync() : creator.Exists());
            }
        }
Beispiel #5
0
        public void Insert_with_Identity_column()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextIdentity(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    context.AddRange(new Blog {
                        Name = "One Unicorn"
                    }, new Blog {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

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

                    Assert.Equal(1, blogs[0].Id);
                    Assert.Equal(2, blogs[1].Id);
                }
            }
        }
Beispiel #6
0
        private static async Task Returns_true_when_database_exists_test(bool async, bool ambientTransaction, bool useCanConnect, bool file)
        {
            using var testDatabase = file
                ? SqlServerTestStore.CreateInitialized("ExistingBloggingFile", useFileName: true)
                : SqlServerTestStore.GetOrCreateInitialized("ExistingBlogging");
            using var context = new BloggingContext(testDatabase);
            var creator = GetDatabaseCreator(context);

            await context.Database.CreateExecutionStrategy().ExecuteAsync(
                async() =>
            {
                using (CreateTransactionScope(ambientTransaction))
                {
                    if (useCanConnect)
                    {
                        Assert.True(async ? await creator.CanConnectAsync() : creator.CanConnect());
                    }
                    else
                    {
                        Assert.True(async ? await creator.ExistsAsync() : creator.Exists());
                    }
                }
            });

            Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
        }
Beispiel #7
0
        private static async Task Delete_database_test(bool async, bool open, bool ambientTransaction, bool file)
        {
            using (var testDatabase = SqlServerTestStore.CreateInitialized("EnsureDeleteBlogging" + (file ? "File" : ""), file))
            {
                if (!open)
                {
                    testDatabase.CloseConnection();
                }

                using (var context = new BloggingContext(testDatabase))
                {
                    var creator = GetDatabaseCreator(context);

                    Assert.True(async ? await creator.ExistsAsync() : creator.Exists());

                    using (CreateTransactionScope(ambientTransaction))
                    {
                        if (async)
                        {
                            Assert.True(await context.Database.EnsureDeletedAsync());
                        }
                        else
                        {
                            Assert.True(context.Database.EnsureDeleted());
                        }
                    }

                    Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);

                    Assert.False(async ? await creator.ExistsAsync() : creator.Exists());

                    Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
                }
            }
        }
        public void Insert_and_update_with_computed_column()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextComputedColumn(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    var blog = context.Add(
                        new FullNameBlog
                    {
                        FirstName = "One",
                        LastName  = "Unicorn"
                    }).Entity;

                    context.SaveChanges();

                    Assert.Equal("One Unicorn", blog.FullName);
                }

                using (var context = new BlogContextComputedColumn(testStore.Name))
                {
                    var blog = context.FullNameBlogs.Single();

                    Assert.Equal("One Unicorn", blog.FullName);

                    blog.LastName = "Pegasus";

                    context.SaveChanges();

                    Assert.Equal("One Pegasus", blog.FullName);
                }
            }
        }
        public void Insert_with_sequence_HiLo()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextHiLo(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    context.AddRange(
                        new Blog
                    {
                        Name = "One Unicorn"
                    }, new Blog
                    {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

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

                    Assert.Equal(1, blogs[0].Id);
                    Assert.Equal(2, blogs[0].OtherId);
                    Assert.Equal(3, blogs[1].Id);
                    Assert.Equal(4, blogs[1].OtherId);
                }
            }
        }
        public void Insert_with_default_string_value_from_sequence()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextStringDefaultValue(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    context.AddRange(
                        new BlogWithStringKey
                    {
                        Name = "One Unicorn"
                    }, new BlogWithStringKey
                    {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContextStringDefaultValue(testStore.Name))
                {
                    var blogs = context.StringyBlogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal("i77", blogs[0].Id);
                    Assert.Equal("i78", blogs[1].Id);
                }
            }
        }
Beispiel #11
0
        public void Insert_with_explicit_with_default_keys()
        {
            using var testStore = SqlServerTestStore.CreateInitialized(DatabaseName);
            using (var context = new BlogContextNoKeyGenerationNullableKey(testStore.Name))
            {
                context.Database.EnsureCreatedResiliently();

                context.AddRange(
                    new NullableKeyBlog {
                    Id = 0, Name = "One Unicorn"
                },
                    new NullableKeyBlog {
                    Id = 1, Name = "Two Unicorns"
                });

                context.SaveChanges();
            }

            using (var context = new BlogContextNoKeyGenerationNullableKey(testStore.Name))
            {
                var blogs = context.NullableKeyBlogs.OrderBy(e => e.Id).ToList();

                Assert.Equal(0, blogs[0].Id);
                Assert.Equal(1, blogs[1].Id);
            }
        }
    public void Insert_ulong_enum_to_Identity_column()
    {
        using var testStore = SqlServerTestStore.CreateInitialized(DatabaseName);
        using (var context = new BlogContextULongEnumToIdentity(testStore.Name))
        {
            context.Database.EnsureCreatedResiliently();

            context.AddRange(
                new BlogWithULongEnumKey {
                Name = "One Unicorn"
            }, new BlogWithULongEnumKey {
                Name = "Two Unicorns"
            });

            context.SaveChanges();
        }

        using (var context = new BlogContextULongEnumToIdentity(testStore.Name))
        {
            var blogs = context.EnumBlogs.OrderBy(e => e.Id).ToList();

            Assert.Equal(1, (int)blogs[0].Id);
            Assert.Equal(2, (int)blogs[1].Id);
        }
    }
        public void Can_initialize_PersistedGrantDbContext()
        {
            using var testDatabase = SqlServerTestStore.CreateInitialized("IdentityServerPersistedGrantDbContext");
            var options = CreateOptions(testDatabase);

            using (var context = new PersistedGrantDbContext(options, new OperationalStoreOptions()))
            {
                context.Database.EnsureCreatedResiliently();
            }
        }
        public void Insert_with_default_value_from_sequence()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextDefaultValue(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    context.AddRange(
                        new Blog
                    {
                        Name = "One Unicorn"
                    }, new Blog
                    {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

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

                    Assert.Equal(0, blogs[0].Id);
                    Assert.Equal(1, blogs[1].Id);
                }

                using (var context = new BlogContextDefaultValueNoMigrations(testStore.Name))
                {
                    context.AddRange(
                        new Blog
                    {
                        Name = "One Unicorn"
                    }, new Blog
                    {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

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

                    Assert.Equal(0, blogs[0].Id);
                    Assert.Equal(1, blogs[1].Id);
                    Assert.Equal(2, blogs[2].Id);
                    Assert.Equal(3, blogs[3].Id);
                }
            }
        }
        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 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_readonly()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextNonKeyReadOnlyDefaultValue(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    context.AddRange(
                        new Blog
                    {
                        Name = "One Unicorn"
                    },
                        new Blog
                    {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();

                    Assert.NotEqual(new DateTime(), context.Blogs.ToList()[0].CreatedOn);
                }

                DateTime dateTime0;

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

                    dateTime0 = blogs[0].CreatedOn;

                    Assert.NotEqual(new DateTime(), dateTime0);
                    Assert.NotEqual(new DateTime(), blogs[1].CreatedOn);

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

                    context.SaveChanges();
                }

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

                    Assert.Equal(dateTime0, blogs[0].CreatedOn);
                    Assert.Equal(new DateTime(1973, 9, 3, 0, 10, 0), blogs[1].CreatedOn);
                }
            }
        }
Beispiel #17
0
        public void Insert_with_client_generated_GUID_key()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                Guid afterSave;
                using (var context = new BlogContextClientGuidKey(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    var blog = context.Add(new GuidBlog {
                        Name = "One Unicorn"
                    }).Entity;

                    var beforeSave      = blog.Id;
                    var beforeSaveNotId = blog.NotId;

                    Assert.NotEqual(default, beforeSave);
Beispiel #18
0
        public void Insert_with_ValueGeneratedOnAdd_GUID_nonkey_property_throws()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextClientGuidNonKey(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    var blog = context.Add(new GuidBlog {
                        Name = "One Unicorn"
                    }).Entity;

                    Assert.Equal(default(Guid), blog.NotId);

                    // No value set on a required column
                    Assert.Throws <DbUpdateException>(() => context.SaveChanges());
                }
            }
        }
        public void Insert_and_update_with_computed_column_with_function()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextComputedColumnWithFunction(testStore.Name))
                {
                    context.Database.ExecuteSqlRaw
                    (
                        @"CREATE FUNCTION
[dbo].[GetFullName](@First NVARCHAR(MAX), @Second NVARCHAR(MAX))
RETURNS NVARCHAR(MAX) WITH SCHEMABINDING AS BEGIN RETURN @First + @Second END");

                    context.GetService <IRelationalDatabaseCreator>().CreateTables();
                }

                using (var context = new BlogContextComputedColumnWithFunction(testStore.Name))
                {
                    var blog = context.Add(
                        new FullNameBlog
                    {
                        FirstName = "One",
                        LastName  = "Unicorn"
                    }).Entity;

                    context.SaveChanges();

                    Assert.Equal("OneUnicorn", blog.FullName);
                }

                using (var context = new BlogContextComputedColumnWithFunction(testStore.Name))
                {
                    var blog = context.FullNameBlogs.Single();

                    Assert.Equal("OneUnicorn", blog.FullName);

                    blog.LastName = "Pegasus";

                    context.SaveChanges();

                    Assert.Equal("OnePegasus", blog.FullName);
                }
            }
        }
Beispiel #20
0
        public void Resolve_concurreny()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextConcurrencyWithRowversion(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    var blog = context.Add(new ConcurrentBlog {
                        Name = "One Unicorn"
                    }).Entity;

                    context.SaveChanges();

                    using (var innerContext = new BlogContextConcurrencyWithRowversion(testStore.Name))
                    {
                        var updatedBlog = innerContext.ConcurrentBlogs.Single();
                        updatedBlog.Name = "One Pegasus";
                        innerContext.SaveChanges();
                        var currentTimestamp = updatedBlog.Timestamp.ToArray();

                        try
                        {
                            blog.Name = "One Earth Pony";
                            context.SaveChanges();
                        }
                        catch (DbUpdateConcurrencyException)
                        {
                            // Update origianal values (and optionally any current values)
                            // Would normally do this with just one method call
                            context.Entry(blog).Property(e => e.Id).OriginalValue        = updatedBlog.Id;
                            context.Entry(blog).Property(e => e.Name).OriginalValue      = updatedBlog.Name;
                            context.Entry(blog).Property(e => e.Timestamp).OriginalValue = updatedBlog.Timestamp;

                            context.SaveChanges();

                            Assert.NotEqual(blog.Timestamp, currentTimestamp);
                        }
                    }
                }
            }
        }
Beispiel #21
0
        public void Insert_explicit_value_into_computed_column()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextComputedColumn(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    context.Add(new FullNameBlog {
                        FirstName = "One", LastName = "Unicorn", FullName = "Gerald"
                    });

                    // The property 'FullName' on entity type 'FullNameBlog' is defined to be read-only before it is
                    // saved, but its value has been set to something other than a temporary or default value.
                    Assert.Equal(
                        CoreStrings.PropertyReadOnlyBeforeSave("FullName", "FullNameBlog"),
                        Assert.Throws <InvalidOperationException>(() => context.SaveChanges()).Message);
                }
            }
        }
Beispiel #22
0
        public void Insert_explicit_value_throws_when_readonly_sequence_before_save()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextReadOnlySequenceKeyColumnWithDefaultValue(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    context.AddRange(new Blog {
                        Id = 1, Name = "One Unicorn"
                    }, new Blog {
                        Name = "Two Unicorns"
                    });

                    // The property 'Id' on entity type 'Blog' is defined to be read-only before it is
                    // saved, but its value has been set to something other than a temporary or default value.
                    Assert.Equal(
                        CoreStrings.PropertyReadOnlyBeforeSave("Id", "Blog"),
                        Assert.Throws <InvalidOperationException>(() => context.SaveChanges()).Message);
                }
            }
        }
Beispiel #23
0
        public void Insert_with_explicit_default_keys()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContext(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    context.AddRange(new Blog {
                        Id = 0, Name = "One Unicorn"
                    }, new Blog {
                        Id = 1, Name = "Two Unicorns"
                    });

                    // DbUpdateException : An error occurred while updating the entries. See the
                    // inner exception for details.
                    // SqlException : Cannot insert explicit value for identity column in table
                    // 'Blog' when IDENTITY_INSERT is set to OFF.
                    Assert.Throws <DbUpdateException>(() => context.SaveChanges());
                }
            }
        }
Beispiel #24
0
        public void Insert_with_server_generated_GUID_key()
        {
            using (var testStore = SqlServerTestStore.CreateInitialized(DatabaseName))
            {
                Guid afterSave;
                using (var context = new BlogContextServerGuidKey(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    var blog = context.Add(new GuidBlog {
                        Name = "One Unicorn"
                    }).Entity;

                    var beforeSave      = blog.Id;
                    var beforeSaveNotId = blog.NotId;

                    Assert.NotEqual(default(Guid), beforeSave);
                    Assert.Equal(default(Guid), beforeSaveNotId);

                    context.SaveChanges();

                    afterSave = blog.Id;
                    var afterSaveNotId = blog.NotId;

                    Assert.NotEqual(default(Guid), afterSave);
                    Assert.NotEqual(default(Guid), afterSaveNotId);
                    Assert.NotEqual(beforeSave, afterSave);
                    Assert.NotEqual(beforeSaveNotId, afterSaveNotId);
                }

                using (var context = new BlogContextServerGuidKey(testStore.Name))
                {
                    Assert.Equal(afterSave, context.GuidBlogs.Single().Id);
                }
            }
        }
Beispiel #25
0
 public CompositeKeyEndToEndTest()
 {
     TestStore = SqlServerTestStore.CreateInitialized("CompositeKeyEndToEndTest");
 }
 public SequenceEndToEndTest()
 {
     TestStore = SqlServerTestStore.CreateInitialized("SequenceEndToEndTest");
 }
Beispiel #27
0
        public void Insert_and_update_with_computed_column_with_querying_function()
        {
            using var testStore = SqlServerTestStore.CreateInitialized(DatabaseName);
            using (var context = new BlogContextComputedColumn(testStore.Name))
            {
                context.GetService <IRelationalDatabaseCreator>().CreateTables();

                context.Database.ExecuteSqlRaw("ALTER TABLE dbo.FullNameBlogs DROP COLUMN FullName;");

                context.Database.ExecuteSqlRaw(
                    @"CREATE FUNCTION [dbo].[GetFullName](@Id int)
RETURNS NVARCHAR(MAX) WITH SCHEMABINDING AS
BEGIN
    DECLARE @FullName NVARCHAR(MAX);
    SELECT @FullName = [FirstName] + [LastName] FROM [dbo].[FullNameBlogs] WHERE [Id] = @Id;
    RETURN @FullName
END");

                context.Database.ExecuteSqlRaw("ALTER TABLE dbo.FullNameBlogs ADD FullName AS [dbo].[GetFullName]([Id]); ");
            }

            try
            {
                using (var context = new BlogContextComputedColumn(testStore.Name))
                {
                    var blog = context.Add(
                        new FullNameBlog {
                        FirstName = "One", LastName = "Unicorn"
                    }).Entity;

                    context.SaveChanges();

                    Assert.Equal("OneUnicorn", blog.FullName);
                }

                using (var context = new BlogContextComputedColumn(testStore.Name))
                {
                    var blog = context.FullNameBlogs.Single();

                    Assert.Equal("OneUnicorn", blog.FullName);

                    blog.LastName = "Pegasus";

                    context.SaveChanges();

                    Assert.Equal("OnePegasus", blog.FullName);
                }

                using (var context = new BlogContextComputedColumn(testStore.Name))
                {
                    var blog1 = context.Add(
                        new FullNameBlog {
                        FirstName = "Hank", LastName = "Unicorn"
                    }).Entity;
                    var blog2 = context.Add(
                        new FullNameBlog {
                        FirstName = "Jeff", LastName = "Unicorn"
                    }).Entity;

                    context.SaveChanges();

                    Assert.Equal("HankUnicorn", blog1.FullName);
                    Assert.Equal("JeffUnicorn", blog2.FullName);
                }
            }
            finally
            {
                using var context = new BlogContextComputedColumn(testStore.Name);
                context.Database.ExecuteSqlRaw("ALTER TABLE dbo.FullNameBlogs DROP COLUMN FullName;");
                context.Database.ExecuteSqlRaw("DROP FUNCTION [dbo].[GetFullName];");
            }
        }
Beispiel #28
0
 public ComputedColumnTest()
 {
     TestStore = SqlServerTestStore.CreateInitialized("ComputedColumnTest");
 }
 public DefaultValuesTest()
 {
     TestStore = SqlServerTestStore.CreateInitialized("DefaultValuesTest");
 }
        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);
            }
        }