Warm tip: This article is reproduced from serverfault.com, please click

Access database cannot create table with default datetime in migration

发布于 2020-11-03 12:40:06

I have some trouble getting the migrations to create default datetime values for my tables. I am using the EF Core Jet provider and need to use MS Access-MDB-Files.

My Connection string is the following:

Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=4;User ID=Admin; Data Source=<my-mdb-file.mdb>

I want to set the default datetime of an property to the time "15:30". I tried the following things:

  • entity.Property(e => e.EndTime).HasDefaultValueSql("#01.01.1900 15:30:00#");
  • entity.Property(e => e.EndTime).HasDefaultValueSql("#01.01.1900 15:30#");
  • entity.Property(e => e.EndTime).HasDefaultValueSql("#15:30:00#");
  • entity.Property(e => e.EndTime).HasDefaultValueSql("#15:30#");
  • entity.Property(e => e.EndTime).HasDefaultValueSql("#01/01/1900 03:30:00#");
  • entity.Property(e => e.EndTime).HasDefaultValueSql("#01/01/1900 03:30#");
  • entity.Property(e => e.EndTime).HasDefaultValueSql("#03:30:00#");
  • entity.Property(e => e.EndTime).HasDefaultValueSql("#03:30#");
  • entity.Property(e => e.EndTime).HasDefaultValue(new TimeSpan(15,30,0));
  • entity.Property(e => e.EndTime).HasDefaultValue(new TimeSpan(3,0,0));

And always get the following error message:

System.Data.OleDb.OleDbException (0x80040E14): Syntax error in CREATE TABLE statement.

Then I dug into the source code of EntityFrameworkCore.Jet and found in the following comment in the class JetMigrationsSQLGenerator: // Jet does not support defaults for hh:mm:ss in create table statement

But in our old code base we do exactly this with an ADO-Driver (it is an legacy Delphi application).

So I have the following questions:

  • Am I wrong with my assertion that this is possible in general?
  • Is the behaviour implemented in the EF-Core Jet provider wrong?
  • And most importantly: How can I get this working?

Edit: To clarify my steps, I always deleted the Migrations folder after changing the model builder configuration and used the functions Add-Migration <MigrationName>and Update-Databaseto test this.

Edit 2: It seems like the comment from the EF Core provider is not totally wrong. Combined datetimes like #12.30.1899 03:30:00# which get created by using TimeSpan are not supported by Access. But if enter the create statement into Access directly and just use the time (#03:30:00#) it works. But I cannot get EF Core to create the statement like this.

Edit 3: In the cases where I used the function HasDefaultValueSqlthe reason for the wrong syntax seems to be an bug in the EF Jet Provider. When this function is used the created SQL statement ends with DEFAULT ('#<value>#'), the problem with this is that the parenthesis are not accepted. This seems to be a know issue.

Edit 4: There was an issue concerning my applications culture. I changed it and now I can get EF Core to generate DateTimes successfully, but these are missing the time values due to the implementation of the Jet-provider. So the current situation is as follows: If I use

  • DateTime I can indeed call Update-Database successfully, but have no time values, only date
  • TimeSpan the Update-Database call fails (I suspect that Access does not like dates with time)
  • HasDefaultValueSql("#03:30:00#") I also get an exception when calling UpdateDatabase

I think that the EF Core Jet Provider is the problem here, but I am not totally convinced.

Questioner
Tobias
Viewed
0
Tobias 2021-01-14 18:09:06

The problem was indeed a combination of multiple factors, including an issue in the provider which was fixed after a report in the github repository.