Skip to content

Can't scaffold migrations seeding data for non-standard types (e.g. NodaTime) #575

Closed

Description

@githubfanster

NodaTime.Instant as shadow property HasData problem vs no problem seeding data via 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 further down 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() 
                });
}

It was mentioned in #502 that I may not be using UseNodaTime for my model, but this is the code in my DbContextOptionsBuilderExtensions class:

public static DbContextOptions DevPgOpts(
    this DbContextOptionsBuilder optionsBuilder,
    bool logSql = true,
    bool clientEvalWarn = false, 
    bool enableSensitiveLog = false)
{
    var connectionString = Configuration.GetConnectionString("DevPgConnect");

    optionsBuilder.UseNpgsql(connectionString, o => o.UseNodaTime());

    if (logSql)
    {
        //... elided for brevity
    }
    //...elided for brevity
}

and i call this method in my AppDbContext class:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        optionsBuilder.DevPgOpts(true, true, true);
    }
}

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 WORKS FINE. NO PROBLEM ENCOUNTERED:

[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

Activity

  1. roji commented on Aug 12, 2018

    @roji
    Member

    Thanks, this is indeed an issue, and the limitation is on the EF Core side rather than in Npgsql - I've opened dotnet/efcore#12979. Let's see what they say.

  2. changed the title [-]NodaTime.Instant as shadow property HasData problem vs no problem seeding data via DbSet.Add[/-] [+]Can't scaffold migrations seeding data for non-standard types (e.g. NodaTime)[/+] on Aug 12, 2018
  3. roji commented on Sep 1, 2018

    @roji
    Member

    Closing as this is currently an EF Core limitation, follow dotnet/efcore#8741 for progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions