Coder Social home page Coder Social logo

pomelo.entityframeworkcore.extensions.tosql's Introduction

Pomelo.EntityFrameworkCore.Extensions.ToSql

Sample

image

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace Sample
{
    enum Sex
    {
        Male,
        Female
    }

    class TestModel
    {
        public Guid Id { get; set; }

        public string Title { get; set; }

        public int Count { get; set; }

        public Sex Sex { get; set; }

        public DateTime Time { get; set; }
    }

    class SqlServerContext : DbContext
    {
        public DbSet<TestModel> Models { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlServer("Server=localhost;uid=sa;pwd=123456;database=tosqltest");
        }
    }
    class MySqlContext : DbContext
    {
        public DbSet<TestModel> Models { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseMySql("Server=localhost;uid=root;pwd=123456;database=tosqltest");
        }
    }

    class PostgreSQLContext : DbContext
    {
        public DbSet<TestModel> Models { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseNpgsql("Server=localhost;uid=postgres;pwd=123456;database=tosqltest");
        }
    }

    class GroupType
    {
        public Sex Key { get; set; }

        public int Count { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var SqlServerContext = new SqlServerContext();
            var SqlServerQuery = SqlServerContext.Models
                .Where(x => x.Title.Contains("Pomelo"))
                .Where(x => new[] { 2, 3, 5, 7, 11 }.Contains(x.Count))
                .Where(x => x.Title.Skip(3).Take(4).ToString() == "Hello");
            Console.WriteLine("SQL Server Generated:");
            Console.WriteLine(SqlServerQuery.ToSql());
            Console.WriteLine("Unevaluated:");
            Console.WriteLine(string.Join(
                Environment.NewLine, 
                SqlServerQuery
                    .ToUnevaluated()));
            Console.WriteLine();

            var MySqlContext = new MySqlContext();
            var MySqlQuery = MySqlContext.Models
                .Where(x => x.Title.Contains("Pomelo"))
                .Where(x => new[] { 2, 3, 5, 7, 11 }.Contains(x.Count));
            Console.WriteLine("MySQL Generated:");
            Console.WriteLine(MySqlQuery.ToSql());
            Console.WriteLine("Unevaluated:");
            Console.WriteLine(string.Join(
                Environment.NewLine, 
                MySqlQuery
                    .ToUnevaluated()));
            Console.WriteLine();

            var PostgreSQLContext = new PostgreSQLContext();
            var PostgreSQLQuery = PostgreSQLContext.Models
                .Where(x => x.Title.Contains("Pomelo"))
                .Where(x => new[] { 2, 3, 5, 7, 11 }.Contains(x.Count))
                .GroupBy(x => x.Sex)
                .Select(x => new GroupType { Key = x.Key, Count = Math.Abs(x.Count()) });
            Console.WriteLine("PostgreSQL Generated:");
            Console.WriteLine(PostgreSQLQuery.ToSql());
            Console.WriteLine("Unevaluated:");
            Console.WriteLine(string.Join(
                Environment.NewLine, 
                PostgreSQLQuery
                    .ToUnevaluated()));
            Console.WriteLine();

            Console.Read();
        }
    }
}

pomelo.entityframeworkcore.extensions.tosql's People

Contributors

yukozh avatar

Watchers

 avatar

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.