Giter Club home page Giter Club logo

pocos-generator's Introduction

Pocos Generator

When all you want are Pocos

If you prefer to work with a micro-orm like Dapper, but hate manually writing POCO classes for your tables, this is the right tool for you.
A port of EZPoco T4 templates to .Net Core 2.0+ using dotnet-script.

Generates POCO classes from your database tables and views. For SQL Server, also generates a class with names of stored procedures, and a class with the name and columns of table-valued parameters.

Getting Started

Prerequisites

The only prerequisite is dotnet-script.
Follow the install instructions for your platform.

Usage

PocosGenerator works in an opt-in style, regarding your database tables. By default all tables are ignored, you have to manually enable the ones you want to generate a POCO for.

In the PocosGenerator.csx file, edit the following section:

if (generator.ReadSchema()){

    // Let's remove ignore for tables and views we need
    /*
        generator.Tables["tablename"].Ignore = false;
    */

    generator.Tables["Users"].Ignore = false;

    /*
        // Tweak Schema
        generator.Tables["tablename"].Ignore = true;                        // To ignore a table
        generator.Tables["tablename"].ClassName = "newname";                // To change the class name of a table
        generator.Tables["tablename"]["columnname"].Ignore = true;          // To ignore a column
        generator.Tables["tablename"]["columnname"].PropertyName="newname"; // To change the property name of a column
        generator.Tables["tablename"]["columnname"].PropertyType="bool";    // To change the property type of a column
    */

    generator.GenerateClass();
}

You can execute the script from command line:

dotnet script PocosGenerator.csx -- output:MyModels.cs connectionstring:ConnectionStrings:MyDatabase config:..\Config\connectionstrings.json

Parameters are in form of param:value, any order.

Parameter Default Purpose
output DbModels.cs Name of created file.
config appsettings.json Config file to read the connection string from. Relative to script location.
connectionstring ConnectionStrings:DefaultConnection The connection string to use for the database.
namespace Models .Net Namespace of the generated classes.
pocos true Generate POCOs.
views true Also include views. If false, only tables are considered.
schema null Restrict to a specific schema (ex. "dbo.").
classprefix null Add a prefix to the generated class names (ex. "AccountingTableName").
classsufix null Add a sufix to the generated class names (ex. "TableNameReporting").
spclass null Name of the class holding the stored procedures. Not generated if null.
tvpclass null Name of the class holding the table valued parameters. Not generated if null.
dapper false Decorate POCOs with attributes for Dapper.Contrib. Nuget package must be included in your project.

You can also run it from Visual Studio Code. Install the C# for Visual Studio Code plugin.
To create the launch.json file for you environment, first run this command in the folder containing the .csx files:

dotnet script init

When running from VS Code, set your options in the PocosGenerator.csx file, this section:

if (!Args.Any()){
    options.Output = "DbModels.cs";
    options.ConfigFilePath = @"..\Config\connectionstrings.json";
    options.Namespace = "Db.Models";
    options.ConnectionStringName = "ConnectionStrings:MyDatabase";
    options.SpClass = "spData";
    options.TvpClass = "tvpData";
}

Example usage of the generated classes with Dapper

        // _connection is an SqlConnection
        public async Task<IEnumerable<Role>> GetAllRoles()
        {
            return await _connection.QueryAsync<Role>(
                spData.dbo_UsersRole_GetAll,
                commandType: CommandType.StoredProcedure
                );
        }

This script was tested with SQL Server only. To make it work with other databases, edit the PocosGenerator.GetDbConnection() method and return a correct DbConnection instance.

pocos-generator's People

Contributors

attilaszasz 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.