Code Monkey home page Code Monkey logo

efcoreextensions's Introduction

efcoreextensions

support for OPENJSON sql server syntax with linq to entities through EFCore

Context

  • Your project use EF Core (2.1.1).
  • You need some JSON features of SQL Server (JSON_VALUE, JSON_QUERY, OPENJSON).
  • You don't want to use FromSql You can use this extension library with a new extension method ValueFromOpenJson.

Configuration

You have to declare some fake DbSet (due to actual limitation of EFCore) :

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<JsonResult<string>>();
    modelBuilder.Entity<JsonResult<int>>();
    modelBuilder.Entity<JsonResult<bool>>();
}

You can use SQLServer or InMemory provider :

optionsBuilder.UseExtensions(extensions =>
{
    //extensions.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=testdbapp2;Trusted_Connection=True;");
    extensions.UseInMemoryDatabase("inmemory");
});

Usages

If you want to EF Core generate valid SQL with right usage of OPENJSON, you have to use the method ValueFromOpenJson on a fake DbSet of JsonResult. You have to declare this DbSet as a variable :

var json = appContext.Set<JsonResult<string>>();

Next you can query a real DbSet (mapped to an existing table) :

var query = appContext.Persons
    .Where(p => json.ValueFromOpenJson(p.Kinds, "$").Select(jr => jr.Value).Contains("kind2"));

The variable query is a classic IQueryable, you trigger the SQL Command with ToList or ToListAsync from EFCore :

var result = await query.ToListAsync();

The SQL generated :

SELECT [p].[Id], [p].[Kinds], [p].[Name]
FROM [Persons] AS [p]
WHERE N'kind2' IN (
    SELECT [jr].[Value]
    FROM (
        SELECT [Key], [Value], [Type] FROM OPENJSON([p].[Kinds], N'$')
    ) AS [jr]
)

TODOs

  • declare JsonResult DbSet automaticaly
  • SQLite support

efcoreextensions's People

Contributors

jgiacomini avatar thomasouvre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

jgiacomini

efcoreextensions's Issues

LOG: server process (PID 13723) was terminated by signal 11: Segmentation fault

Dear Team,

We are getting error LOG: server process (PID 13723) was terminated by signal 11: Segmentation fault in our production environment database
we are using postgresql database version 9.6.5.
The database will started automatically but we want to know about root cause of this error and
how we prevent this error in future

kindly attention Mr.CregRinger Sir

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.