19

I am trying to use the new DateOnly aspects of c# but when I come to do my migrations I am having the following issue. I am using SQL Server 2019 the error is.

'Amenitie.StartDate could not be mapped because it is of type 'DateOnly', which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the'

My model is as follows

public class Amenitie
{
    public int? Id { get; set; }
    public Rooms? Rooms { get; set; }
    public string? Description { get; set; }
    public DateOnly StartDate { get; set; }
    public DateOnly EndDate { get; set; }
    public bool? isAvailable { get; set; }
    public bool? isDeleted { get; set; }
    public bool? isActive { get; set; }
    public DateTime? LastModifiedBy { get; set; }
    public DateTime? CreateDate { get; set; }
}

My Dal project and my web project are both set to

<PropertyGroup>
  <TargetFramework>net6.0</TargetFramework>
  <Nullable>enable</Nullable>
</PropertyGroup>

I am using the following package versions.

<ItemGroup>
 <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
 <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.9" />
 <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.9" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.9">
 <PrivateAssets>all</PrivateAssets>
 <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
 </PackageReference>
 <PackageReference Include="Microsoft.EntityFrameworkCore.Relational.Design" Version="2.0.0-preview1-final" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="2.0.0-preview1-final" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.9">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
 </PackageReference>
 <PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
</ItemGroup>

Is DateOnly available in EF Core 6 and above I presume?

1
  • Well, considering the DateOnly type is new for .NET 6, that seems like a reasonable presumption. Have you tried applying the ColumnAttribute and specifying that those fields are date fields?
    – Tieson T.
    Sep 11, 2021 at 20:36

2 Answers 2

23

Try this

      public class YourDbContext : DbContext
      {
            Ctors() {...}

        protected override void ConfigureConventions(ModelConfigurationBuilder builder)
        {
            builder.Properties<DateOnly>()
                .HaveConversion<DateOnlyConverter>()
                .HaveColumnType("date");
        }
      }
              
      /// <summary>
      /// Converts <see cref="DateOnly" /> to <see cref="DateTime"/> and vice versa.
      /// </summary>
      public class DateOnlyConverter : ValueConverter<DateOnly, DateTime>
      {
          /// <summary>
          /// Creates a new instance of this converter.
          /// </summary>
          public DateOnlyConverter() : base(
                  d => d.ToDateTime(TimeOnly.MinValue),
                  d => DateOnly.FromDateTime(d))
          { }
      }

EDIT: with RC2 this does not work for nullable types.

EDIT 2: with NET6 release version this works for nullable types again, and more than that, you dont need to have a separate converter for "DateOnly?" anymore.

2
10

You have two choices:

  1. use DateTime instead of DateOnly.

  2. build a custom converter (see below).

As far I can see, the actual version of Entity Framework Core issue tracker states that model builder does not support it (find the issue here). Building a converter may solve your issue (cited from there):

protected override void ConfigureConventions(ModelConfigurationBuilder builder)
{
   builder.Properties<DateOnly>()
          .HaveConversion<DateOnlyConverter, DateOnlyComparer>()
          .HaveColumnType("date");
}  
6
  • Then why do they have it their seems pointless. Sep 11, 2021 at 21:15
  • 5
    .NET 6 and EF Core are two different projects with two different teams.
    – boj
    Sep 11, 2021 at 21:19
  • 3
    Support must be added to the underlying ADO.NET provider first (Microsoft.Data.SqlClient)
    – ErikEJ
    Sep 12, 2021 at 5:52
  • 1
    "Then why do they have it their seems pointless." You need to have the type itself available before adding support for various database providers. Func fact: The above solution does currently not work in RC2 for Nullable DateOnly. See the linked issue for more details.
    – CleanCoder
    Oct 20, 2021 at 9:30
  • 4
    @boj mysql and postgresql map dateonly just fine
    – cikatomo
    Dec 12, 2021 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.