Skip to content

Function StoreType is ignored for DateTime #27954

@zykindmitry

Description

@zykindmitry

I Have an Entity:

public class MyEntity 
{
    public int Id { get; set; } 
    public DateTime SomeDate { get; set; }
    public static DateTime Modify(DateTime date) => throw new NotSupportedException();
}

with configuration:

modelBuilder.ToTable("MyEntity", "dbo");
modelBuilder.HasKey(x => x.Id);
modelBuilder.Property(x => x.SomeDate).HasColumnType("datetime");

SQL scalar value function:
function [dbo].[ModifyDate] (@dt datetime) returns datetime

and a mapping to static method

modelBuilder
                .HasDbFunction(
                    typeof(MyEntity).GetMethod(nameof(MyEntity.Modify)))
                .HasName("ModifyDate")
                .HasStoreType("datetime")
                .HasSchema("dbo");

LINQ to SQL query:

var date = new DateTime(...);
DbSet<MyEntity>().Where(x => x.SomeDate == date).ToArray();

works correctly, but

DbSet<MyEntity>().Where(x => MyEntity.Modify(x.SomeDate) == date).ToArray();

fails with SqlException saying that it cannot convert string to a datetime.

The reason of SqlException is that ef core ignores function StoreType and converts 'date' to DateTime2
string representation 2011-05-15T18:54:00.0000000Z while in the first case it is 2011-05-15T18:54:00.000

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions