简体   繁体   English

Entity Framework Core 3.1 脚手架生成隐藏列

[英]Entity Framework Core 3.1 Scaffolding Generating HIDDEN Column

I have this table that uses a temporal table.我有这张使用时态表的表。 I have marked [ValidFromUtc] and [ValidTillUtc] as HIDDEN so when I scaffold the table using entity framework it will not generate these properties in the entity.我已将 [ValidFromUtc] 和 [ValidTillUtc] 标记为 HIDDEN,因此当我使用实体框架搭建表格时,它不会在实体中生成这些属性。

CREATE TABLE [User].[User](
    [Username] [varchar](10) NOT NULL,
    [FirstName] [nvarchar](55) NULL,
    [LastName] [nvarchar](55) NULL,
    [EmailAddress] [varchar](55) NULL,
    [IsActive] [bit] NULL,
    [ValidFromUtc] [datetime2](7) GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
    [ValidTillUtc] [datetime2](7) GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
    [ModifiedBy] [nvarchar](128) NOT NULL,
    [CreateSid] [nvarchar](128) NOT NULL,
    [LastSid]  AS (suser_sname()),
 CONSTRAINT [User_pk] PRIMARY KEY CLUSTERED 
(
    [Username] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = ON, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
    PERIOD FOR SYSTEM_TIME ([ValidFromUtc], [ValidTillUtc])
) ON [PRIMARY]
WITH
(
SYSTEM_VERSIONING = ON ( HISTORY_TABLE = [User].[UserHistory] )
)
GO

ALTER TABLE [User].[User] ADD  CONSTRAINT [DF__User_ValidFromUtc]  DEFAULT (sysutcdatetime()) FOR [ValidFromUtc]
GO

ALTER TABLE [User].[User] ADD  CONSTRAINT [DF__User_ValidTillUtc]  DEFAULT ('9999-12-31 23:59:59.9999999') FOR [ValidTillUtc]
GO

ALTER TABLE [User].[User] ADD  CONSTRAINT [DF__User_ModifiedBy]  DEFAULT (suser_sname()) FOR [ModifiedBy]
GO

ALTER TABLE [User].[User] ADD  CONSTRAINT [DF__User_CreateSid]  DEFAULT (suser_sname()) FOR [CreateSid]
GO

This works fine in ef core 2.1 when scaffolding from the database table (no hidden columns):当从数据库表(无隐藏列)搭建脚手架时,这在 ef core 2.1 中工作正常:

 modelBuilder.Entity<User>(entity =>
            {
                entity.HasKey(e => e.Username)
                    .HasName("User_pk");

                entity.ToTable("User", "User");

                entity.Property(e => e.Username)
                    .HasMaxLength(10)
                    .IsUnicode(false)
                    .ValueGeneratedNever();

                entity.Property(e => e.CreateSid)
                    .IsRequired()
                    .HasMaxLength(128)
                    .HasDefaultValueSql("(suser_sname())");

                entity.Property(e => e.EmailAddress)
                    .HasMaxLength(55)
                    .IsUnicode(false);

                entity.Property(e => e.FirstName).HasMaxLength(55);

                entity.Property(e => e.LastName).HasMaxLength(55);

                entity.Property(e => e.LastSid)
                    .HasMaxLength(128)
                    .HasComputedColumnSql("(suser_sname())");

                entity.Property(e => e.ModifiedBy)
                    .IsRequired()
                    .HasMaxLength(128)
                    .HasDefaultValueSql("(suser_sname())");
            });

But now that I have updated to ef core 3.1 it adds these columns to the entity and gives them values.但是现在我已经更新到 ef core 3.1,它将这些列添加到实体中并为它们提供值。

 modelBuilder.Entity<User>(entity =>
            {
                entity.HasKey(e => e.Username)
                    .HasName("User_pk");

                entity.ToTable("User", "User");

                entity.Property(e => e.Username)
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.CreateSid)
                    .IsRequired()
                    .HasMaxLength(128)
                    .HasDefaultValueSql("(suser_sname())");

                entity.Property(e => e.EmailAddress)
                    .HasMaxLength(55)
                    .IsUnicode(false);

                entity.Property(e => e.FirstName).HasMaxLength(55);

                entity.Property(e => e.LastName).HasMaxLength(55);

                entity.Property(e => e.LastSid)
                    .HasMaxLength(128)
                    .HasComputedColumnSql("(suser_sname())");

                entity.Property(e => e.ModifiedBy)
                    .IsRequired()
                    .HasMaxLength(128)
                    .HasDefaultValueSql("(suser_sname())");

                entity.Property(e => e.ValidFromUtc).HasDefaultValueSql("(sysutcdatetime())");

                entity.Property(e => e.ValidTillUtc).HasDefaultValueSql("('9999-12-31 23:59:59.9999999')");
            });

I have reviewed the breaking changes for ef core 2.1 to 3.1 but I see no mention of this.我已经查看了 ef core 2.1 到 3.1 的重大更改,但我没有看到任何提及。

Does anyone have any information about this change or why its now ignoring the HIDDEN property.有没有人有任何关于此更改的信息或为什么它现在忽略 HIDDEN 属性。

To conclude this ticket I discovered that 3.1 does not scaffold hidden fields when the compatibility level is equal or greater than 130.总结这张票,我发现当兼容性级别等于或大于 130 时,3.1不会构建隐藏字段。

In entity framework 2.1 it checks the server version (equal or greater than 13) instead of the compatibility level.在实体框架 2.1 中,它检查服务器版本(等于或大于 13)而不是兼容性级别。

In our case we were running Sql Server 2016 (version 13) but with a lower compatibility level (120).在我们的例子中,我们运行的是 Sql Server 2016(版本 13),但兼容性级别较低(120)。 Once we updated the compatibility level to 130 the hidden fields no longer were scaffolded.一旦我们将兼容级别更新到 130,隐藏字段就不再被搭建。

These differences can be seen in the following files in the efCore git repo:这些差异可以在 efCore git 存储库中的以下文件中看到:

efCore 2.1 (line 538) https://github.com/dotnet/efcore/blob/release/2.1/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs efCore 2.1(第 538 行) https://github.com/dotnet/efcore/blob/release/2.1/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs

efCore 3.1 (line 621) https://github.com/dotnet/efcore/blob/release/3.1/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs efCore 3.1(第 621 行) https://github.com/dotnet/efcore/blob/release/3.1/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM