Dapper Plus - Try it Online

CSharp · Console

/Program.cs

// @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 Dapper;
using Microsoft.Data.SqlClient;
using Z.Dapper.Plus;

public class Program
{
    public static void Main()
    {
        var customers = new List<Customer>
        {
            new Customer { Name = "Alice", Email = "[email protected]" },
            new Customer { Name = "Bob", Email = "[email protected]" }
        };

        using var connection = new SqlConnection(
            FiddleHelper.GetConnectionStringSqlServer());

        // Create the table from the Customer class
        connection.CreateTable<Customer>();

        // Insert all customers
        connection.BulkInsert(customers);

        Console.WriteLine($"{customers.Count} customers inserted.");
		FiddleHelper.WriteTable(connection.Query<Customer>("SELECT * FROM Customer"));
    }
}

[Table("Customer")]
public class Customer
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
	public int CustomerID { get; set; }
	public string Name { get; set; }
    public string Email { get; set; }
}
;