AxaFrance.EFCore.SqlExpressions.InMemory 1.0.2

Prefix Reserved
dotnet add package AxaFrance.EFCore.SqlExpressions.InMemory --version 1.0.2                
NuGet\Install-Package AxaFrance.EFCore.SqlExpressions.InMemory -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AxaFrance.EFCore.SqlExpressions.InMemory" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AxaFrance.EFCore.SqlExpressions.InMemory --version 1.0.2                
#r "nuget: AxaFrance.EFCore.SqlExpressions.InMemory, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AxaFrance.EFCore.SqlExpressions.InMemory as a Cake Addin
#addin nuget:?package=AxaFrance.EFCore.SqlExpressions.InMemory&version=1.0.2

// Install AxaFrance.EFCore.SqlExpressions.InMemory as a Cake Tool
#tool nuget:?package=AxaFrance.EFCore.SqlExpressions.InMemory&version=1.0.2                

EntityFrameworkCore.SqlExpressions

EntityFrameworkCore.SqlExpressions permet d'enregistrer des SqlExpressions simplement dans le mecanisme de rendu d' EntityFramework

EntityFrameworkCore.SqlExpressions.InMemory

Il est possible de faire simplement vos tests d'integration via cette librairie InMemory

Usage

Configuration

        
        services.AddDbContextPool<NorthwindContext>(builder =>
            builder.UseSqlServer("Data Source=localhost;Initial Catalog=Northwind;",
                optionsBuilder =>
                {
                    optionsBuilder.UseAddedExpressions(add =>
                    {
                        add(new SqlExpressionConfiguration(CustomSqlDbFunctionsExtensions.DeclaringType,
                            CustomSqlDbFunctionsExtensions.MyCustomMethodInfo,
                            arguments => new MyCustomSqlExpression(arguments)));
                    });
                }));

DbFunctionExtensions

public static class CustomSqlDbFunctionsExtensions
{
    public static Type DeclaringType { get; } = typeof(CustomSqlDbFunctionsExtensions);

    public static MethodInfo MyCustomMethodInfo { get; } =
        DeclaringType.GetMethod(nameof(MyCustom), new[] { typeof(DbFunctions), typeof(int) })!;

    public static string MyCustom(this DbFunctions _, int value) =>
        throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(MyCustom)));
}

Custom SqlExpression

public sealed class MyCustomSqlExpression : SqlExpression
{
    private readonly SqlExpression[] sqlExpressions;

    private static readonly StringTypeMapping relationalTypeMapping;
    
    private static readonly SqlConstantExpression constantExpression;
    
    private static readonly Type stringType;

    public MyCustomSqlExpression(SqlExpression[] sqlExpressions) : base(stringType, relationalTypeMapping)
    {
        this.sqlExpressions = sqlExpressions.Prepend(constantExpression).ToArray();
    }

    static MyCustomSqlExpression()
    {
        relationalTypeMapping = new("nvarchar(max)", DbType.String);
        stringType = typeof(string);
        constantExpression = new SqlConstantExpression(Constant("-> ", stringType), relationalTypeMapping);
    }

    protected override Expression VisitChildren(ExpressionVisitor visitor)
    {
        return new SqlFunctionExpression("CONCAT", this.sqlExpressions, false,
            this.sqlExpressions.Select(_ => false), stringType,
            this.TypeMapping);
    }

    protected override void Print(ExpressionPrinter expressionPrinter)
    {
        expressionPrinter.Visit(this);
    }
}

Query

var result = dbContext.Regions.Select(r => EF.Functions.MyCustom(r.RegionId)).ToList();

License (MIT)

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 171 6/22/2023
1.0.1 158 6/20/2023
1.0.0 156 6/20/2023