简体   繁体   English

Oracle EF Core - 如何配置要自动生成的日期时间列?

[英]Oracle EF Core - how to configure a datetime column to be auto-generated?

Firstly this is just a simple feature that is supported by at least EF Core for SQL Server, but for Oracle (the provider implemented by Oracle), I'm not so sure if it's supported. 首先,这只是一个由至少EF Core for SQL Server支持的简单功能,但对于Oracle(由Oracle实现的提供程序),我不太确定它是否受支持。

Simply I have an entity class like this: 我只有一个像这样的实体类:

public class Item {
    public int Id {get;set;}
    public DateTime CreatedDateUtc {get;set;}
}

The problem is how to configure the EF core to auto-generate database's date to CreatedDateUtc each time inserting without having to set that value on client side? 问题是如何配置EF核心以在每次插入时自动生成数据库的日期到CreatedDateUtc而无需在客户端设置该值?

Here's what I've tried: 这是我尝试过的:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
   modelBuilder.Entity<Item>()
               .Property(e => e.CreatedDateUtc)
               .ValueGeneratedOnAdd();                 
}

But after running add-migration , the generated class is just empty like this: 但是在运行add-migration ,生成的类就像这样是空的:

protected override void Up(MigrationBuilder migrationBuilder)
{

}

protected override void Down(MigrationBuilder migrationBuilder)
{

}

So actually it does nothing with that configuration (not supported?). 实际上它对该配置没有任何作用(不支持?)。 Also the first thing is to generate a datetime value, the second important thing is to generate a UTC datetime value. 第一件事是生成日期时间值,第二件重要的事情是生成UTC日期时间值。 I believe in the feasibility for the first requirement whereas the second seems to be unfeasible at least with Oracle EF Core? 我相信第一个要求的可行性,而第二个似乎至少在Oracle EF Core中是不可行的? (nuget name: Oracle.EntityFrameworkCore ). (nuget名称: Oracle.EntityFrameworkCore )。

If you have some solution to achieve the first requirement, please share it anyway even if the second requirement is impossible. 如果你有一些解决方案来达到第一个要求,请分享它,即使第二个要求是不可能的。 At least we need some work-around then. 至少我们需要一些解决方法。 Thank you. 谢谢。

Try this - 尝试这个 -

protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
          modelBuilder.Entity<Item>()
               .Property(b => b.CreatedDateUtc)
               .HasDefaultValueSql("GETUTCDATE()");
    }

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

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