简体   繁体   English

如何将时态表添加到 EF Code-First DBContext?

[英]How to add temporal tables to EF Code-First DBContext?

I want to add SQL temporal tables to my project in DBContext not in migration file?我想将 SQL 时态表添加到 DBContext 中的项目而不是迁移文件中? How to achieve it?如何实现? Edit: I've added it to seed function but it's still not working编辑:我已将它添加到种子 function 但它仍然无法正常工作

 protected override void Seed(MSDBContext context)
    {
        string sql = @"  ALTER TABLE dbo.Workshops

 ADD SysStartTime datetime2 GENERATED ALWAYS AS ROW START  
  CONSTRAINT DF_Workshops_SysStartTime DEFAULT SYSUTCDATETIME() NOT NULL,
  SysEndTime datetime2 GENERATED ALWAYS AS ROW END 
  CONSTRAINT DF_Workshops_SysEndTime DEFAULT CONVERT (DATETIME2, '9999-12-31 23:59:59') NOT NULL,
         PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime)
  GO

  ALTER TABLE dbo.Workshops
      SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.WorkshopsHistory))
  GO";
        context.Database.ExecuteSqlCommand(sql);

        context.SaveChanges();

        context.Configuration.LazyLoadingEnabled = false;
    }

You can run adhoc sql queries if you wanted to just create tables.如果您只想创建表,可以运行即席 sql 查询。

using (var ctx = new DBDataContext())
{
    var rowsEffected = ctx.Database.ExecuteSqlCommand("YOUR SQL TO ADD TABLES");
}

If this doesn't work, you dont have to use EF, you could use good old ADO.NET sql client classes to run queries.如果这不起作用,您不必使用 EF,您可以使用旧的 ADO.NET sql 客户端类来运行查询。

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

相关问题 如何在 EF 代码优先中禁用链接表的级联删除? - How to disable cascade delete for link tables in EF code-first? 现有数据库的EF Code-First将模型添加到现有DbContext中 - EF Code-First from existing db add models into existing DbContext 具有DbContext和TenantId的MultiTenancy - 拦截器,过滤器,EF代码优先 - MultiTenancy with DbContext and TenantId - Interceptors, Filters, EF Code-First EF Code-First 中查找表的最佳实践 - Best Practices for Lookup Tables in EF Code-First 使用EF 5.x DbContext Fluent Generator从Model-First迁移到Code-First:ComplexTypes - Migrating from Model-First to Code-First: ComplexTypes using EF 5.x DbContext Fluent Generator 继承EF代码优先 - Inheritance EF Code-First DBContext文件是否应该使用EF 6.0.1和ASP.NET 4 Code-First自动更新? - Should the DBContext file get updated automatically using EF 6.0.1 and ASP.NET 4 Code-First? 有没有办法在不向其添加数据的情况下创建数据库 ef 代码优先 - Is there a way to create db without add data to it ef code-first EF代码优先将唯一索引添加到列 - EF Code-First Add Unique Index to Column 如何将我的EF Code-First首先转换为数据库? - How to convert my EF Code-First to Database first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM