Код: Выделить всё
namespace TestEntityFramework;
public sealed class UniversityId(string value)
{
public string Value { get; } = value;
public override string ToString()
{
return Value;
}
}
public static class UniversityIdExtensions
{
public static bool IsForeignUniversity(this UniversityId universityId)
{
return universityId.ToString().Contains('-');
}
}
Код: Выделить всё
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
namespace TestEntityFramework;
public class DatabaseContext(DbContextOptions options) : DbContext(options)
{
public DbSet Students { get; set; }
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
base.ConfigureConventions(configurationBuilder);
configurationBuilder.Properties().HaveConversion();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity().HasKey(s => s.Id);
MethodInfo methodInfo = typeof(UniversityIdExtensions)
.GetMethod(name: nameof(UniversityIdExtensions.IsForeignUniversity), types: [typeof(UniversityId)])!;
modelBuilder
.HasDbFunction(methodInfo)
.HasTranslation(args =>
{
ISqlExpressionFactory sqlExpressionFactory = this.GetService();
SqlConstantExpression likePattern = sqlExpressionFactory.Constant(value: "%-%");
return sqlExpressionFactory.Like(match: args[0], pattern: likePattern);
});
}
}
Код: Выделить всё
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace TestEntityFramework;
public sealed class UniversityIdConverter() : ValueConverter(
convertToProviderExpression: id => id.Value, convertFromProviderExpression: id => new UniversityId(id));
Код: Выделить всё
public static IResult Handler(DatabaseContext context)
{
IQueryable query = context.Students
.Where(s => s.UniversityId.IsForeignUniversity())
.Select(s => new Response(s.Id, s.Name, s.Country, s.UniversityId.ToString()));
return Results.Ok(query);
}
Код: Выделить всё
Unable to create a 'DbContext' of type 'TestEntityFramework.DatabaseContext'. The exception 'The parameter 'universityId' for the DbFunction 'TestEntityFramework.UniversityIdExtensions.IsForeignUniversity(TestEntityFramework.Uni versityId)' has an invalid type 'UniversityId'. Ensure the parameter type can be mapped by the current provider.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https: //go.microsoft.com/fwlink/?linkid=851728
Код: Выделить всё
.HasTranslation(args => new SqlFunctionExpression(
functionName: "LIKE",
arguments:
[
args[0],
new SqlConstantExpression(
constantExpression: Expression.Constant("%-%"),
typeMapping: new StringTypeMapping(storeType: "nvarchar(max)", dbType: null)
)
],
nullable: false,
argumentsPropagateNullability: [false, false],
type: typeof(bool),
typeMapping: RelationalTypeMapping.NullMapping));
Подробнее здесь: https://stackoverflow.com/questions/787 ... by-the-cur