Skip to content

Translate NodaTime's SystemClock.Instance.GetCurrentInstant() #502

Closed

Description

@roji

To get the current time.

Note that like DateTime.Now this must not be evaluatable (i.e. always evaluated at the server), but the current plugin model doesn't allow adding stuff to NpgsqlEvaluatableExpressionFilter.

Activity

  1. added this to the 2.1.2 milestone on Jul 3, 2018
  2. modified the milestones: 2.1.1.1, 2.1.2 on Jul 15, 2018
  3. roji commented on Aug 8, 2018

    @roji
    MemberAuthor

    @austindrenski this seems like a pretty important missing feature, any chance you want to take a stab at it? I'm hoping it wouldn't be too hard.

  4. austindrenski commented on Aug 8, 2018

    @austindrenski
    Contributor

    @roji Sure, I can take a look at it this weekend.

  5. self-assigned this
    on Aug 8, 2018
  6. githubfanster commented on Aug 8, 2018

    @githubfanster

    I wonder if this is the problem I'm encountering.

    i had prepared this text to report my problem, but my problem looks related to this.


    NodaTime.Instant as shadow property HasData problem vs no problem in DbSet.Add

    In the attached minimal code example (it's a zip of a minimal VS2017 solution), my model MyEntity has a "Created" shadow property of type NodaTime.Instant.

    When I try to seed data for MyEntity using HasData, I encounter a problem (stack trace is below).

    This code produces an exception:

    protected override void OnModelCreating(ModelBuilder builder)
            {
                builder.AddShadowPropertiesAllEntities();
                builder.UsePostgresConventions();
                builder.Entity<MyEntity>()
                        .HasData(
                            new { Id = 1, Description = "Blah", Created = SystemClock.Instance.GetCurrentInstant() }
                        );
            }

    The stack trace is:

    F:\vs2017play\NodatimeExpt\App.Data>dotnet ef migrations add init --startup-project ..\StartupConsoleApp
    System.InvalidOperationException: The current CSharpHelper cannot scaffold literals of type 'NodaTime.Instant'. Configure your services to use one that can.
       at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.UnknownLiteral(Object value)
       at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext()
       at System.String.Join(String separator, IEnumerable`1 values)
       at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal(IReadOnlyList`1 values, Boolean vertical)
       at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationOperationGenerator.Generate(InsertDataOperation operation, IndentedStringBuilder builder)
       at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite site, T0 arg0, T1 arg1, T2 arg2)
       at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationOperationGenerator.Generate(String builderName, IReadOnlyList`1 operations, IndentedStringBuilder builder)
       at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMigration(String migrationNamespace, String migrationName, IReadOnlyList`1 upOperations, IReadOnlyList`1 downOperations)
       at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
       at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
    The current CSharpHelper cannot scaffold literals of type 'NodaTime.Instant'. Configure your services to use one that can.
    

    BUT THIS TEST CODE DOES NOT:

    [TestMethod]
            public void TestMethod1()
            {
                var myEntity = new MyEntity { Id = 1, Description = "Blah" };
                AppDbContext.Entry(myEntity).Property("Created").CurrentValue = SystemClock.Instance.GetCurrentInstant();
                AppDbContext.MyEntities.Add(myEntity);
                AppDbContext.SaveChanges();
    
                using (var context = new AppDbContext(dbContextOptionsBuilder.DevPgOpts(true, true, true)))
                {
                    Assert.AreEqual(1, context.MyEntities.Count());
                }
            }

    Any idea what might be happening? Am I doing something wrong?

    Thanks for helping!


    NodatimeExpt.zip

  7. roji commented on Aug 8, 2018

    @roji
    MemberAuthor

    @githubfanster no, this is not related to your problem. This issue is about translating a NodaTime function call to SQL when sending queries, not when inserting data (the update/insert pipeline is very different from the query pipeline).

    It appears that you simply forgot to include UseNodaTime() in your model as indicated in the docs.

    If you're still having trouble, please open a new issue rather than posting here again.

  8. austindrenski commented on Aug 9, 2018

    @austindrenski
    Contributor

    Merged in #577.

  9. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions