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

Function StoreType is ignored for DateTime #27954

Closed
zykindmitry opened this issue May 5, 2022 · 2 comments · Fixed by #27995
Closed

Function StoreType is ignored for DateTime #27954

zykindmitry opened this issue May 5, 2022 · 2 comments · Fixed by #27995
Assignees
Labels
area-query 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

@zykindmitry
Copy link

zykindmitry commented May 5, 2022

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

@roji
Copy link
Member

roji commented May 5, 2022

Could be similar/related to #25980

@smitpatel
Copy link
Contributor

Generated SQL after fix

exec sp_executesql N'SELECT [m].[Id], [m].[SomeDate]
FROM [MyEntities] AS [m]
WHERE [dbo].[ModifyDate]([m].[SomeDate]) = @__date_0',N'@__date_0 datetime',@__date_0='2012-12-12 00:00:00'

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 10, 2022
smitpatel added a commit that referenced this issue May 10, 2022
- Add a convention which configures actual type mapping from store type on DbFunction
- While translating db function use the TypeMapping if available

Resolves #27954
Resolves #27524
smitpatel added a commit that referenced this issue May 11, 2022
- We use TypeMapping if configured on the DbFunction, or
- We try to get it if store type is specified
- If neither of above work then we do the default type mapping based on return type

Resolves #27954
Resolves #27524
smitpatel added a commit that referenced this issue May 11, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-preview5 May 25, 2022
smitpatel added a commit that referenced this issue Sep 27, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-preview5, 6.0.12 Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query 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
Development

Successfully merging a pull request may close this issue.

5 participants