Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

587
Visualizações
How to map time(0) to TimeOnly Entity Framework SQL Server 2019?

I'm trying to use the new TimeOnly type 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.

The property 'PassportOffice.CloseTime' is of type 'DateTime' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

I'm using Entity Framework Core 6. The model is as follows.

public class PassportOffice
{
    public int Id { get; set; }
    [Required]
    public TimeOnly OpenTime { get; set; }
    [Required]
    public TimeOnly CloseTime { get; set; }
}

So I've tried to make TimeOnly converter

public class TimeOnlyConverter : ValueConverter<TimeOnly, DateTime>
{
    public TimeOnlyConverter() : base(
       timeOnly => new DateTime(DateOnly.MinValue.Year, DateOnly.MinValue.Month, DateOnly.MinValue.Day,
            timeOnly.Hour, timeOnly.Minute, timeOnly.Second),
       dateTime => TimeOnly.FromDateTime(dateTime))
   { }
}

And used it in DbContext as follows

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
    configurationBuilder.Properties<TimeOnly>()
        .HaveConversion<TimeOnlyConverter>()
        .HaveColumnType("time(0)");
}

Did I made a mistake? Or is there any other way to solve the problem?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Entity Framework Map CLR type TimeSpan SQL Server type to time

Change the Model Definition as below

public class PassportOffice
    {
        public int Id { get; set; }
        [Required]
        public TimeSpan OpenTime { get; set; }
        [Required]
        public TimeSpan CloseTime { get; set; }
    }

No Need for Converter. it will work as expected

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda