Dapper Plus - CreateTable and Populate

CSharp · Console

/Program.cs

// Dapper Plus
// Doc: https://dapper-plus.net/create-table

// @nuget: Dapper
// @nuget: Microsoft.Data.SqlClient
// @nuget: Z.Dapper.Plus

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.Data.SqlClient;
using Dapper;
using Z.Dapper.Plus;

public class Program
{	
	public static void Main()
	{
		var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer());
		
		var customers = new List<Customer>();
		customers.Add(new Customer() { Name = "Jonathan Magnan", Email = "[email protected]" });
		customers.Add(new Customer() { Name = "ZZZ Projects", Email = "[email protected]" });
		customers.Add(new Customer() { Name = "Sara", Email = "[email protected]" });
		
		connection.CreateTable(customers, CreateTableType.Permanent);
		
		FiddleHelper.WriteTable("CustomCustomer:", connection.Query<Customer>("SELECT CustomerID, FullName as Name, Email, Version FROM CustomerWithAnnotation"));
	}
	
	[Table("CustomerWithAnnotation")]
	public class Customer 
	{
		[Key]
		[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
		public int CustomerID { get; set; }
		[Column("FullName", TypeName="nvarchar(50)")]
		public string Name { get; set; }
		public string Email { get; set; }
		
		[Timestamp]
		public byte[] Version { get; set; }
		
		[NotMapped]
		public string NotMapped { get; set; }
	}
}
;