为了解决我的错误(如下所示),我想知道如何编辑我的实体框架 T4 模板,以便将“ HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed) ”添加到 POCO 中我的临时表的 GENERATED ALWAYS AS ROW START/END 列自动生成类的配置部分,如下所示:Property(x => x.ValidFrom).HasColumnName(@"valid_from").HasColumnType("datetime2").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);Property(x => x.ValidTo).HasColumnName(@"valid_to").HasColumnType("datetime2").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);我可以手动添加它(这解决了我的错误),但是当我保存 T4 模板时它当然会被覆盖。尝试插入记录时出错:“无法将显式值插入表 'my_table' 中的 GENERATED ALWAYS 列。将 INSERT 与列列表一起使用以排除 GENERATED ALWAYS 列,或将 DEFAULT 插入到 GENERATED ALWAYS 列中。”我看到了这篇文章,但它没有帮助(也许它只在使用 edmx 文件时才有效,或者我不理解解决方案):Entity Framework not working with temporal table我的临时表示例:CREATE TABLE [dbo].[my_table]( [id] [int] IDENTITY(1,1) NOT NULL, [user_id] [int] NOT NULL, [valid_from] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL, [valid_to] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL, CONSTRAINT [PK_my_table] PRIMARY KEY CLUSTERED ( [id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], PERIOD FOR SYSTEM_TIME ([valid_from], [valid_to])) ON [PRIMARY]WITH(SYSTEM_VERSIONING = ON ( HISTORY_TABLE = [dbo].[my_table_history] ))GOALTER TABLE [dbo].[my_table] ADD CONSTRAINT [DF_my_table_SysStart] DEFAULT (sysutcdatetime()) FOR [valid_from]GOALTER TABLE [dbo].[my_table] ADD CONSTRAINT [DF_my_table_SysEnd] DEFAULT (CONVERT([datetime2](7),'9999-12-31 23:59:59')) FOR [valid_to]GO插入记录的示例代码:var recordToCreate = new MyTable(){ UserId = info.UserId, ValidFrom = info.ValidFrom, ValidTo = info.ValidTo,};_context.MyTables.Add(recordToCreate);_context.SaveChanges();
1 回答
- 1 回答
- 0 关注
- 124 浏览
添加回答
举报
0/150
提交
取消