Autocreate tables with Dapper ORM in C#.Net

How to create tables automatically with Dapper ORM(SQL SERVER) in C#.Net

Dapper ORM

Dapper is a micro ORM for .Net framework, which help developers for setup database connections based on the model class. I resembles me Prisma ORM in Javascript. All you need a connection string.

I have keep asking a question myself when I mastered the Dapper. Can I build tables automatically, without the long CREATE TABLE statement ?

With a Dapper extension class Z.Dapper.Plus I succeeded to generate data tables on a Microsoft SQL Server.

Install Z.Dapper.Plus

I assume that you have setup the Dapper ORM in your project.

Using the Package Manager Console we can add the library as follows.

NuGet\Install-Package Z.Dapper.Plus -Version 4.0.33

After the installation you were able to use the CreateTable method as follows.

   using (var conn = Connection.OpenDBConnection())
  {
    var r = conn.CreateTable<Product>("products");
                    
                    
  }

The CreateTable will not create primary key for the table.

Here Products is the name of the Model class and products is the name of the table in the SQL server.

I didn't find the CreateTable work on MySQL and SQLite either.

dapper
c#.net
SQL SERVER
ADO.NET

Write your comment