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

c# entity-framework-core oracle

Question

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.

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?

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:

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. I believe in the feasibility for the first requirement whereas the second seems to be unfeasible at least with Oracle EF Core? (nuget name: 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.

1
0
6/19/2019 10:04:21 PM

Accepted Answer

Try this -

protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
          modelBuilder.Entity<Item>()
               .Property(b => b.CreatedDateUtc)
               .HasDefaultValueSql("GETUTCDATE()");
    }
0
6/20/2019 2:07:11 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow