Giter Site home page Giter Site logo

efcoredemo's Introduction

EntityFrameworkCore Demo

EntityFrameworkCore Demo in a WebAPI project.

Dependencies: .NET Core 2.2, Microsoft.EntityFrameworkCore v2.2

Medium Blog Post

Usage

  1. Apply EF Migration to your local SQL Server database

    # if using Package Manger Console in Visual Studio
    Update-Database
    
    # if using console
    dotnet ef database update
  2. Run Web App

    In WebBlogs.Web folder, issue command dotnet watch run, then in browser navigate to http://localhost:5000/swagger/index.html. In Swagger page, you can try out APIs.

    Or, in Visual Studio, click run "IIS Express".

EF Core LINQ Expression

Normal Query

return await _dbContext.Authors
                .Where(x => x.AuthorMembership == AuthorMembership.Gold ||
                            x.AuthorMembership == AuthorMembership.Platinum)
                .Select(x => new AuthorViewModel(x))
                .ToListAsync();
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (44ms) [Parameters=[], CommandType='Text', ommandTimeout='30']
SELECT [x].[Id], [x].[AuthorMembership], [x].[FirstName], [x].LastName]
FROM [Authors] AS [x]
WHERE ([x].[AuthorMembership] = 2) OR ([x].[AuthorMembership] = 3)

Expression

public class Author
{
    ...
    public static Expression<Func<Author, bool>> GoldAndUp = x =>
            x.AuthorMembership == AuthorMembership.Gold ||
            x.AuthorMembership == AuthorMembership.Platinum;
    ...
}
return await _dbContext.Authors
                .Where(Author.GoldAndUp)
                .Select(x => new AuthorViewModel(x))
                .ToListAsync();
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (4ms) [Parameters=[], CommandType='Text', ommandTimeout='30']
SELECT [x].[Id], [x].[AuthorMembership], [x].[FirstName], [x].LastName]
FROM [Authors] AS [x]
WHERE ([x].[AuthorMembership] = 2) OR ([x].[AuthorMembership] = 3)

Expression and Projection

await _dbContext.Authors.Select(x => new AuthorViewModel(x)).ToListAsync();
Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [x].[Id], [x].[AuthorMembership], [x].[FirstName], [x].[LastName]
FROM [Authors] AS [x]
public static Expression<Func<Author, AuthorViewModel>> Projection1
            => x => new AuthorViewModel
            {
                Id = x.Id,
                FirstName = x.FirstName,
                LastName = x.LastName
            };

await _dbContext.Authors.Select(AuthorViewModel.Projection1).ToListAsync();
Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [x].[Id], [x].[FirstName], [x].[LastName]
FROM [Authors] AS [x]
public static Expression<Func<Author, AuthorViewModel>> Projection2
            => x => new AuthorViewModel(x);

return await _dbContext.Authors.Select(AuthorViewModel.Projection2).ToListAsync();
Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [x].[Id], [x].[AuthorMembership], [x].[FirstName], [x].[LastName]
FROM [Authors] AS [x]
var authors = await _dbContext.Authors.ToListAsync();
return authors.Select(AuthorViewModel.Projection1.Compile()).ToList();

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.