Skip to content

Commit

Permalink
Ignore hidden columns (used in SQL Server system versioned tables)
Browse files Browse the repository at this point in the history
fixes #8192
  • Loading branch information
Erik Ejlskov Jensen authored and lajones committed Apr 27, 2017
1 parent 4d96eb0 commit 3647f57
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ public virtual DatabaseModel Create(DbConnection connection, TableSelectionSet t
private string TemporalTableWhereClause =>
_serverVersion?.Major >= 13 ? " AND t.temporal_type <> 1" : string.Empty;

private string IsHiddenColumnWhereClause =>
_serverVersion?.Major >= 13 ? " AND c.is_hidden = 0" : string.Empty;

private void GetDefaultSchema()
{
var command = _connection.CreateCommand();
Expand Down Expand Up @@ -338,7 +341,8 @@ RIGHT JOIN (SELECT * FROM sys.indexes WHERE is_primary_key = 1) AS i ON i.object
LEFT JOIN sys.computed_columns cc ON cc.object_id = c.object_id AND cc.column_id = c.column_id
JOIN sys.tables AS t ON t.object_id = c.object_id
WHERE t.name <> '" + HistoryRepository.DefaultTableName + "'" +
TemporalTableWhereClause;
TemporalTableWhereClause +
IsHiddenColumnWhereClause;

using (var reader = command.ExecuteReader())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;

namespace E2ETest.Namespace
{
public partial class SystemVersioned
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace E2ETest.Namespace
{
public partial class SystemVersionedContext : DbContext
{
public virtual DbSet<SystemVersioned> SystemVersioned { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer(@"{{connectionString}}");
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SystemVersioned>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedNever();

entity.Property(e => e.Name)
.IsRequired()
.HasColumnType("varchar(50)");
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,60 @@ ON FilteredIndex (Number) WHERE Number > 10
}
}

[ConditionalFact]
[SqlServerCondition(SqlServerCondition.SupportsHiddenColumns)]
public void Hidden_Column()
{
using (var scratch = SqlServerTestStore.Create("WithHiddenColumns"))
{
scratch.ExecuteNonQuery(@"
CREATE TABLE dbo.SystemVersioned
(
Id int NOT NULL PRIMARY KEY CLUSTERED,
Name varchar(50) NOT NULL,
SysStartTime datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
SysEndTime datetime2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
PERIOD FOR SYSTEM_TIME(SysStartTime, SysEndTime)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.History));
");

var configuration = new ReverseEngineeringConfiguration
{
ConnectionString = scratch.ConnectionString,
ProjectPath = TestProjectDir + Path.DirectorySeparatorChar,
ProjectRootNamespace = TestNamespace,
ContextClassName = "SystemVersionedContext",
UseFluentApiOnly = true
};
var expectedFileSet = new FileSet(new FileSystemFileService(),
Path.Combine("ReverseEngineering", "Expected"),
contents => contents.Replace("{{connectionString}}", scratch.ConnectionString))
{
Files = new List<string>
{
"SystemVersionedContext.expected",
"SystemVersioned.expected",
}
};

var filePaths = Generator.GenerateAsync(configuration).GetAwaiter().GetResult();

scratch.ExecuteNonQuery(@"
ALTER TABLE dbo.SystemVersioned SET (SYSTEM_VERSIONING = OFF);
DROP TABLE dbo.History;
");

var actualFileSet = new FileSet(InMemoryFiles, Path.GetFullPath(TestProjectDir))
{
Files = new[] { filePaths.ContextFile }.Concat(filePaths.EntityTypeFiles).Select(Path.GetFileName).ToList()
};

AssertEqualFileContents(expectedFileSet, actualFileSet);
AssertCompile(actualFileSet);
}
}

protected override ICollection<BuildReference> References { get; } = new List<BuildReference>
{
#if NET46
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public bool IsMet
{
isMet &= TestEnvironment.GetFlag(nameof(SqlServerCondition.SupportsOffset)) ?? true;
}
if (Conditions.HasFlag(SqlServerCondition.SupportsHiddenColumns))
{
isMet &= TestEnvironment.GetFlag(nameof(SqlServerCondition.SupportsHiddenColumns)) ?? false;
}
if (Conditions.HasFlag(SqlServerCondition.SupportsMemoryOptimized))
{
isMet &= TestEnvironment.GetFlag(nameof(SqlServerCondition.SupportsMemoryOptimized)) ?? false;
Expand Down Expand Up @@ -72,6 +76,7 @@ public enum SqlServerCondition
IsSqlAzure = 1 << 2,
IsNotSqlAzure = 1 << 3,
SupportsMemoryOptimized = 1 << 4,
SupportsAttach = 1 << 5
SupportsAttach = 1 << 5,
SupportsHiddenColumns = 1 << 6
}
}
3 changes: 2 additions & 1 deletion test/EFCore.SqlServer.FunctionalTests/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"ElasticPoolName": "",
"SupportsSequences": true,
"SupportsOffset": true,
"SupportsMemoryOptimized": false
"SupportsMemoryOptimized": false,
"SupportsHiddenColumns": false
}
}
}

0 comments on commit 3647f57

Please sign in to comment.