Git Product home page Git Product logo

entityframeworkcore.sqlserver.jsonextention's Introduction

MsSql Json Extention for Entity Framework Core 6

This extention aim is define json columns and querying them.

Get it for .Net 6

PM> Install-Package EntityFrameworkCore.SqlServer.JsonExtention -Version 2.0.0

Get it for .Net 3.1

PM> Install-Package EntityFrameworkCore.SqlServer.JsonExtention -Version 1.0.0

Basic usage

Add Option Builder First

Inside dbcontext OnConfiguring method, call UseJsonExtention()

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
    base.OnConfiguring(optionsBuilder);
    optionsBuilder.UseJsonFunctions();
}

Add Json Conversion

Using Fluent api in model builder and just call HasJsonConversion() method.

modelBuilder.Entity<Country>().Property(p => p.CountryDetail).HasJsonConversion();

Supported Conversion

The library supports List<>, Dictionary<string,object> and custom entity types.

Example entities:

public class Country
{
    public int Id { get; set; }
    public string Name { get; set; }
    public CountryDetail CountryDetail { get; set; }
    public Dictionary<string, object> ExtraInformation { get; set; }
    public List<string> OfficialLanguages { get; set; }
    public List<int> UtcTimeZones { get; set; }
}

public class CountryDetail
{
    public string Code { get; set; }
    public DateTime? Founded { get; set; }
    public List<City> Cities { get; set; }
}

public class City
{
    public string Name { get; set; }
    public DateTime? Founded { get; set; }
    public int? Population { get; set; }
}

And usage in dbcontext:

public class TestContext : DbContext {
    public DbSet<Country> Countries { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
        base.OnConfiguring(optionsBuilder);
        optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=JsonExtentionTest;Integrated Security=True",
            providerOptions => providerOptions.CommandTimeout(60));

        optionsBuilder.UseJsonFunctions();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder.Entity<Country>().Property(p => p.CountryDetail).HasJsonConversion();
        modelBuilder.Entity<Country>().Property(p => p.ExtraInformation).HasJsonConversion();
        modelBuilder.Entity<Country>().Property(p => p.UtcTimeZones).HasJsonConversion();
        modelBuilder.Entity<Country>().Property(p => p.OfficialLanguages).HasJsonConversion();
    }
}

Querying

Supported Json functions are:

ISJSON    : Tests whether a string contains valid JSON.
JSON_VALUE: Extracts a scalar value from a JSON string.
JSON_QUERY: Extracts an object or an array from a JSON string.

Mssql documentation

ISJSON

var isJsons = _context.Countries.Select(s => EF.Functions.IsJson(s.CountryDetail)).ToList();

Generated SQL query

SELECT ISJSON([c].[CountryDetail])
FROM [Countries] AS [c]

JSON_VALUE

var phones = _context.Countries
                .Where(w => EF.Functions.JsonValue(w.ExtraInformation, "InternetTLD") != null)
                .OrderByDescending(o => EF.Functions.JsonValue(o.ExtraInformation, "InternetTLD"))
                .Select(s => EF.Functions.JsonValue(s.ExtraInformation, "InternetTLD"))
                .ToList();

Generated SQL query

SELECT JSON_VALUE([c].[ExtraInformation], '$.InternetTLD')
FROM [Countries] AS [c]
WHERE JSON_VALUE([c].[ExtraInformation], '$.InternetTLD') IS NOT NULL
ORDER BY JSON_VALUE([c].[ExtraInformation], '$.InternetTLD') DESC

JSON_QUERY

var entityId = 1;

var result = _context.Countries.Where(w => w.Id == entityId).Select(s => EF.Functions.JsonQuery(s.CountryDetail, "Cities")).FirstOrDefault();

var cities = JsonSerializer.Deserialize<List<City>>(result);

Generated SQL query

SELECT TOP(1) JSON_QUERY([c].[CountryDetail], '$.Cities')
FROM [Countries] AS [c]
WHERE [c].[Id] = 1

entityframeworkcore.sqlserver.jsonextention's People

Contributors

iozcelik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

entityframeworkcore.sqlserver.jsonextention's Issues

Receive following Error on Asp.net Core 5

TypeLoadException: Method 'Translate' in type 'EntityFrameworkCore.SqlServer.JsonExtention.JsonTranslator' from assembly 'EntityFrameworkCore.SqlServer.JsonExtention, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.