Ejemplo n.º 1
0
        public void Update_explicit_value_in_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"
                    });

                    context.SaveChanges();
                }

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

                    blog.FullName = "The Gorilla";

                    // The property 'FullName' on entity type 'FullNameBlog' is defined to be read-only after it has been saved,
                    // but its value has been modified or marked as modified.
                    Assert.Equal(
                        CoreStrings.PropertyReadOnlyAfterSave("FullName", "FullNameBlog"),
                        Assert.Throws <InvalidOperationException>(() => context.SaveChanges()).Message);
                }
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
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);
                }
            }
        }
Ejemplo n.º 4
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];");
            }
        }