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

[release/6.0] Query: Use type mapping from IDbFunction while translating (#27995) #29216

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,16 @@ public RelationalMethodCallTranslatorProvider(RelationalMethodCallTranslatorProv
arguments,
dbFunction.IsNullable,
argumentsPropagateNullability,
method.ReturnType.UnwrapNullableType())
method.ReturnType.UnwrapNullableType(),
dbFunction.TypeMapping)
: _sqlExpressionFactory.Function(
dbFunction.Schema,
dbFunction.Name,
arguments,
dbFunction.IsNullable,
argumentsPropagateNullability,
method.ReturnType.UnwrapNullableType());
method.ReturnType.UnwrapNullableType(),
dbFunction.TypeMapping);
}

return _plugins.Concat(_translators)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestUtilities;
Expand Down Expand Up @@ -51,6 +52,56 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
mb.ToTable((string)null);
}
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task StoreType_for_UDF_used(bool async)
{
var contextFactory = await InitializeAsync<Context27954>();
using var context = contextFactory.CreateContext();

var date = new DateTime(2012, 12, 12);
var query1 = context.Set<MyEntity>().Where(x => x.SomeDate == date);
var query2 = context.Set<MyEntity>().Where(x => MyEntity.Modify(x.SomeDate) == date);

if (async)
{
await query1.ToListAsync();
await Assert.ThrowsAnyAsync<Exception>(() => query2.ToListAsync());
}
else
{
query1.ToList();
Assert.ThrowsAny<Exception>(() => query2.ToList());
}
}

protected class Context27954 : DbContext
{
public Context27954(DbContextOptions options)
: base(options)
{
}

public DbSet<MyEntity> MyEntities { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.HasDbFunction(typeof(MyEntity).GetMethod(nameof(MyEntity.Modify)))
.HasName("ModifyDate")
.HasStoreType("datetime")
.HasSchema("dbo");
}
}

protected class MyEntity
{
public int Id { get; set; }
[Column(TypeName = "datetime")]
public DateTime SomeDate { get; set; }
public static DateTime Modify(DateTime date) => throw new NotSupportedException();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,24 @@ HAVING COUNT(*) = 1
ORDER BY [t].[ParcelNumber]");
}

public override async Task StoreType_for_UDF_used(bool async)
{
await base.StoreType_for_UDF_used(async);

AssertSql(
@"@__date_0='2012-12-12T00:00:00.0000000' (DbType = DateTime)

SELECT [m].[Id], [m].[SomeDate]
FROM [MyEntities] AS [m]
WHERE [m].[SomeDate] = @__date_0",
//
@"@__date_0='2012-12-12T00:00:00.0000000' (DbType = DateTime)

SELECT [m].[Id], [m].[SomeDate]
FROM [MyEntities] AS [m]
WHERE [dbo].[ModifyDate]([m].[SomeDate]) = @__date_0");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Muliple_occurrences_of_FromSql_in_group_by_aggregate(bool async)
Expand Down