TSQLGenCore 1.0.1

dotnet add package TSQLGenCore --version 1.0.1
                    
NuGet\Install-Package TSQLGenCore -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TSQLGenCore" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TSQLGenCore" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="TSQLGenCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TSQLGenCore --version 1.0.1
                    
#r "nuget: TSQLGenCore, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TSQLGenCore@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TSQLGenCore&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=TSQLGenCore&version=1.0.1
                    
Install as a Cake Tool

TSQL Generator

Is a standard .NET library to help handling and create on-the-fly SQL commands like insert, update, a basic select and delete. It's very useful to use in conjunction with Dapper and/or ADO.NET directly with a Repository or UnitOfWork approach.

Basic usage

Either build source code and add via reference or download in NuGet. There are 2 projects, a Annotation library and the Generator library. You just need Annotation library on your models project. In your data (or repository) project you will need both.

Creating a base class

First you need to create a basic class (or model), like this:

using System;
namespace TSQLGen.Console.Models
{
    public class SampleModel
    {
        public Int32 Id { get; set; }
        public String Name { get; set; }
        public String Code { get; set; }
        public DateTime Date { get; set; }
        public Boolean Active { get; set; }
    }
}
Add annotations

Add data annotations on your properties:

using System;
using TSQLGen.Annotations;

namespace TSQLGen.Console.Models
{
    [Table("MY_SQL_TABLE_NAME")]
    public class SampleModel
    {
        [Field(true)]
        public Guid Id { get; set; }

        [Field]
        public String Name { get; set; }

        [Field]
        public String Code { get; set; }

        [Field]
        public DateTime Date { get; set; }

        [Field]
        public Boolean Active { get; set; }


        public Object ExcludeThisProperty { get; set; }
    }
}

Table annotation will be use to generate the table name. Field annotation marks a property to be proccessed. Properties without this annotation will be ignored. Field annotation also has:

  • fieldName: create a custom generated name (different from the property name.
  • isPrimaryKey: to mark a property as primary key on generated command, mostly used on delete and update generated commands.
  • isAutoGenerated: indicates this field is auto-generated meaning it won't be added on generated command (update and insert commands).
Generate SQL commands

Generate commands by calling specific methods:

var tsqlGen = new TSQLGen.TSQLGen<SampleModel>(TSQLGen.DatabaseType.MySQL);

String insertCommand = tsqlGen.GetInsertCommand();
String updateCommand = tsqlGen.GetUpdateCommand(null);
String deleteCommand = tsqlGen.GetDeleteCommand();
String selectAllCommand = tsqlGen.GetSelectAllCommand();
String selectByKeyCommand = tsqlGen.GetSelectByKeyCommand();

A insert command generated above will be produced like this:

insert into MY_SQL_TABLE_NAME (Name, Code, Date, Active) values (@Name, @Code, @Date, @Active)

Other options

There is a few options do customize generated SQLs:

Custom parameter prefix

Set ParameterPrefix to customize the leading character in parameter field (the "@" in the example above).

Scalar and Auto generated value queries

AutoGeneratedQuery is a option that you can add custom scalar value, mostly used with Dapper or pure ADO.NET connections; very useful when you have a auto-generated value column on your database.

var tsqlGen = new TSQLGen.TSQLGen<TSQLGen.Console.Models.SampleModel>(TSQLGen.DatabaseType.MSSQL);

//Get last number inserted
tsqlGen.AutoGeneratedQuery = "SELECT SCOPE_IDENTITY()";
Custom table and field name enclosure characters

Custom character that delimiter table and column names can be set via:

var tsqlGen = new TSQLGen.TSQLGen<TSQLGen.Console.Models.SampleModel>(TSQLGen.DatabaseType.MSSQL);

tsqlGen.FieldEnclosureStart = "[";
tsqlGen.FieldEnclosureEnd = "]";

tsqlGen.TableEnclosureStart = "[";
tsqlGen.TableEnclosureEnd = "]";
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 1,546 10/5/2021