Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Literal generation fails for DateOnly/TimeOnly #26156

Closed
ajcvickers opened this issue Sep 23, 2021 · 0 comments · Fixed by #26159 or PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#1517
Assignees
Labels
area-type-mapping closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@ajcvickers
Copy link
Contributor

Originally filed by @aderrose as PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#1513

When trying to add a migration with dotnet ef migrations add 'Initial', the following exception is being thrown:

System.NotSupportedException: The type mapping for 'TimeOnly' has not implemented code literal generation.
   at Microsoft.EntityFrameworkCore.Storage.CoreTypeMapping.GenerateCodeLiteral(Object value)
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.UnknownLiteral(Object value)
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Array(Type type, IEnumerable values, Boolean vertical)
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal[T](T[] 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, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace IssueConsoleTemplate
{
    public class SunTime
    {
        public int ID { get; set; }
        public DateOnly EntryDate { get; set; } // <-- issue
        public DateTime Sunrise { get; set; }
        public DateTime Sunset { get; set; }
        public TimeOnly DayLength { get; set; } // <-- issue
    }

    public class Context : DbContext
    {
        public DbSet<SunTime> SunTimes { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //var connectionString = "server=127.0.0.1;port=3308;user=root;password=;database=Issue1513";
            //var serverVersion = ServerVersion.AutoDetect(connectionString);
            
            optionsBuilder
                //.UseMySql(connectionString, serverVersion)
                .UseSqlite(@"Data Source=Issue1513.db")
                //.UseNpgsql(@"server=127.0.0.1;port=5432;user id=postgres;password=postgres;database=Issue1513")
                .UseLoggerFactory(
                    LoggerFactory.Create(
                        b => b
                            .AddConsole()
                            .AddFilter(level => level >= LogLevel.Information)))
                .EnableSensitiveDataLogging()
                .EnableDetailedErrors();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<SunTime>(
                entity =>
                {
                    entity.HasData(
                        new SunTime
                        {
                            ID = 1,
                            EntryDate = new DateOnly(2021, 09, 23),
                            Sunrise = new DateTime(2021, 09, 23, 6, 54, 0),
                            Sunset = new DateTime(2021, 09, 23, 19, 2, 0),
                            DayLength = TimeOnly.FromTimeSpan(
                                new DateTime(2021, 09, 23, 19, 2, 0) -
                                new DateTime(2021, 09, 23, 6, 54, 0))
                        });
                });
        }
    }

    internal static class Program
    {
        private static void Main()
        {
            using var context = new Context();

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            var sunTimes = context.SunTimes.ToList();
            
            Trace.Assert(sunTimes.Count == 1);
            Trace.Assert(sunTimes[0].ID == 1);
        }
    }
}
@roji roji self-assigned this Sep 23, 2021
roji added a commit that referenced this issue Sep 23, 2021
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 23, 2021
@ajcvickers ajcvickers added this to the 6.0.0 milestone Sep 24, 2021
lauxjpn added a commit to lauxjpn/Pomelo.EntityFrameworkCore.MySql that referenced this issue Nov 9, 2021
lauxjpn added a commit to PomeloFoundation/Pomelo.EntityFrameworkCore.MySql that referenced this issue Nov 9, 2021
* Remove workaround for dotnet/efcore#26156.

* Update dependencies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-type-mapping closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
2 participants