CSharp · Console
// Dapper Plus
// Doc: https://dapper-plus.net/dapper-plus-context
// @nuget: Dapper
// @nuget: Microsoft.Data.SqlClient
// @nuget: Z.Dapper.Plus
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Microsoft.Data.SqlClient;
using Dapper;
using Z.Dapper.Plus;
public class Program
{
public static ProductContext SavingProductContext = new ProductContext();
public static void Main()
{
var products = new List<Product>();
products.Add(new Product() { ProductID = 13, Name = "Dapper Plus", Description = @"Use <a href=""https://dapper-plus.net/"" target=""_blank"">Dapper Plus</a> to extend your IDbConnection with high-performance bulk operations." });
products.Add(new Product() { ProductID = 66, Name = "C# Eval Expression", Description = @"Use <a href=""https://eval-expression.net/"" target=""_blank"">C# Eval Expression</a> to compile and execute C# code at runtime." });
products.Add(new Product() { ProductID = 99, Name = "Entity Framework Extensions", Description = @"Use <a href=""https://entityframework-extensions.net/"" target=""_blank"">Entity Framework Extensions</a> to extend your DbContext with high-performance bulk operations." });
var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer());
connection.CreateTable<Product>();
SavingProductContext.BulkInsert(products);
// Query data with Dapper
FiddleHelper.WriteTable(connection.Query<Product>("SELECT * FROM Product"));
}
public class ProductContext : DapperPlusContext
{
public ProductContext() : base(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
{
this.Entity<Product>().Identity(x => x.ProductID, true);
}
}
[Table("Product")]
public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string CreatedUser { get; set; }
}
}