AxaFrance.EFCore.SqlExpressions.InMemory 1.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package AxaFrance.EFCore.SqlExpressions.InMemory --version 1.0.0                
NuGet\Install-Package AxaFrance.EFCore.SqlExpressions.InMemory -Version 1.0.0                
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.0" />                
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.0                
#r "nuget: AxaFrance.EFCore.SqlExpressions.InMemory, 1.0.0"                
#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.0

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

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 169 6/22/2023
1.0.1 156 6/20/2023
1.0.0 154 6/20/2023