How can I use SQL Server JSON_VALUE function in EF 6 Code First for classic .NET

Jonatan Dragon

How can I use SQL Server JSON_VALUE function in EF 6 Code First for classic .NET? I found I can do it in EF Core like this:

public static class JsonExtensions
{
    public static string JsonValue(string column, [NotParameterized] string path)
    {
        throw new NotSupportedException();
    }
}

// In OnModelCreating
modelBuilder.HasDbFunction(typeof(JsonExtensions).GetMethod(nameof(JsonExtensions.JsonValue)))
    .HasName("JSON_VALUE")
    .HasSchema("");

// And then the usage
var result = db.Blogs.Select(t => JsonExtensions.JsonValue(t.Log, "$.JsonPropertyName")).ToArray();

But how can I achieve this in EF 6 in classic .NET (in my case, its 4.6.1)?

Jonatan Dragon

In classic .NET it's a little bit different, but still possible like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Add(new RegisterJsonValueFunctionConvention());
}

// Than define your function
[DbFunction("CodeFirstDatabaseSchema", "JSON_VALUE")]
public static string JsonValue(string expression, string path)
{
    throw new NotSupportedException();
}

Then, because JSON_VALUE is not defined in the Entity Framework SQL Server provider manifest, you have to create IStoreModelConvention like this:

public class RegisterJsonValueFunctionConvention : IStoreModelConvention<EdmModel>
{
    public void Apply(EdmModel item, DbModel model)
    {
        var expressionParameter = FunctionParameter.Create("expression", GetStorePrimitiveType(model, PrimitiveTypeKind.String), ParameterMode.In);
        var pathParameter = FunctionParameter.Create("path", GetStorePrimitiveType(model, PrimitiveTypeKind.String), ParameterMode.In);
        var returnValue = FunctionParameter.Create("result", GetStorePrimitiveType(model, PrimitiveTypeKind.String), ParameterMode.ReturnValue);
        CreateAndAddFunction(item, "JSON_VALUE", new[] { expressionParameter, pathParameter }, new[] { returnValue });
    }

    protected EdmFunction CreateAndAddFunction(EdmModel item, string name, IList<FunctionParameter> parameters, IList<FunctionParameter> returnValues)
    {
        var payload = new EdmFunctionPayload { StoreFunctionName = name, Parameters = parameters, ReturnParameters = returnValues, Schema =  GetDefaultSchema(item), IsBuiltIn = true };
        var function = EdmFunction.Create(name, GetDefaultNamespace(item), item.DataSpace, payload, null);
        item.AddItem(function);
        return function;
    }

    protected EdmType GetStorePrimitiveType(DbModel model, PrimitiveTypeKind typeKind)
    {
        return model.ProviderManifest.GetStoreType(TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(typeKind))).EdmType;
    }

    protected string GetDefaultNamespace(EdmModel layerModel)
    {
        return layerModel.GlobalItems.OfType<EdmType>().Select(t => t.NamespaceName).Distinct().Single();
    }

    protected string GetDefaultSchema(EdmModel layerModel)
    {
        return layerModel.Container.EntitySets.Select(s => s.Schema).Distinct().SingleOrDefault();
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

EF6 Code First and mapping via annotations, how do I build the configuration?

分類Dev

How can I avoid duplicate rows in foreign table with EF Code First

分類Dev

SQL Server 2012のJSON_VALUE?

分類Dev

How to create the sequence and apply two tables in SQL Server using Entity framework 6 code first approach?

分類Dev

How can I get rid of yield and use another function instead in my code

分類Dev

How can I read SQL Server filestream with ASP.NET Core

分類Dev

How can I use Azure App Configuration service within Azure Function v1 (.NET Framework)

分類Dev

how can I use void** function(void**)

分類Dev

How can I use 'Generate Scripts' in SQL Server 2017 with Graph Database Objects?

分類Dev

How can I use a CASE condition based on a junction table in SQL Server?

分類Dev

Set EF6 Code First strings fluently to nvarchar(max)

分類Dev

Creating a many-to-many relationship in EF6 code first

分類Dev

Functional grouping in DbContext for EF6 using FluentAPI and Code First

分類Dev

Custom value type, EF Code First and routing

分類Dev

Can I use FOR JSON without a source column name becoming a key in SQL Server 2016?

分類Dev

Can I use temporary value in sql query

分類Dev

Can I use regex capturing groups in SQL Server 2014?

分類Dev

how can i use `#function` symbol in a `inline` function?

分類Dev

How to make EF.net filter all the data on SQL Server and not on C#

分類Dev

Entity framework Code First SQL Server View

分類Dev

How to fill a Select with data from SQL Server? Using ASP Classic

分類Dev

How can I force the X server to use my nvidia card?

分類Dev

How can i use celery with different code base in API and workers

分類Dev

How can I use JavaScript private fields in VS Code?

分類Dev

How can I "install" fonts to use in Visual Studio Code?

分類Dev

How can I use code snippets in F#

分類Dev

how to use two useState in one function so that the second useState can use the first updated useState?

分類Dev

How can I change properties of FK in SQL Server database project?

分類Dev

SQL Server : how can I filter a many to many relationship with delimiter

Related 関連記事

  1. 1

    EF6 Code First and mapping via annotations, how do I build the configuration?

  2. 2

    How can I avoid duplicate rows in foreign table with EF Code First

  3. 3

    SQL Server 2012のJSON_VALUE?

  4. 4

    How to create the sequence and apply two tables in SQL Server using Entity framework 6 code first approach?

  5. 5

    How can I get rid of yield and use another function instead in my code

  6. 6

    How can I read SQL Server filestream with ASP.NET Core

  7. 7

    How can I use Azure App Configuration service within Azure Function v1 (.NET Framework)

  8. 8

    how can I use void** function(void**)

  9. 9

    How can I use 'Generate Scripts' in SQL Server 2017 with Graph Database Objects?

  10. 10

    How can I use a CASE condition based on a junction table in SQL Server?

  11. 11

    Set EF6 Code First strings fluently to nvarchar(max)

  12. 12

    Creating a many-to-many relationship in EF6 code first

  13. 13

    Functional grouping in DbContext for EF6 using FluentAPI and Code First

  14. 14

    Custom value type, EF Code First and routing

  15. 15

    Can I use FOR JSON without a source column name becoming a key in SQL Server 2016?

  16. 16

    Can I use temporary value in sql query

  17. 17

    Can I use regex capturing groups in SQL Server 2014?

  18. 18

    how can i use `#function` symbol in a `inline` function?

  19. 19

    How to make EF.net filter all the data on SQL Server and not on C#

  20. 20

    Entity framework Code First SQL Server View

  21. 21

    How to fill a Select with data from SQL Server? Using ASP Classic

  22. 22

    How can I force the X server to use my nvidia card?

  23. 23

    How can i use celery with different code base in API and workers

  24. 24

    How can I use JavaScript private fields in VS Code?

  25. 25

    How can I "install" fonts to use in Visual Studio Code?

  26. 26

    How can I use code snippets in F#

  27. 27

    how to use two useState in one function so that the second useState can use the first updated useState?

  28. 28

    How can I change properties of FK in SQL Server database project?

  29. 29

    SQL Server : how can I filter a many to many relationship with delimiter

ホットタグ

アーカイブ