Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
dapper.rainbow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Dapper;
// to have a play, install Dapper.Rainbow from nuget
namespace TestDapper
{
class Program
{
// no decorations, base class, attributes, etc
class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime? LastPurchase { get; set; }
}
// container with all the tables
class MyDatabase : Database<MyDatabase>
{
public Table<Product> Products { get; set; }
}
static void Main(string[] args)
{
var cnn = new SqlConnection("Data Source=.;Initial Catalog=tempdb;Integrated Security=True");
cnn.Open();
var db = MyDatabase.Init(cnn, commandTimeout: 2);
try
{
db.Execute("waitfor delay '00:00:03'");
}
catch (Exception)
{
Console.WriteLine("yeah ... it timed out");
}
db.Execute("if object_id('Products') is not null drop table Products");
db.Execute(@"create table Products (
Id int identity(1,1) primary key,
Name varchar(20),
Description varchar(max),
LastPurchase datetime)");
int? productId = db.Products.Insert(new {Name="Hello", Description="Nothing" });
var product = db.Products.Get((int)productId);
product.Description = "untracked change";
// snapshotter tracks which fields change on the object
var s = Snapshotter.Start(product);
product.LastPurchase = DateTime.UtcNow;
product.Name += " World";
// run: update Products set LastPurchase = @utcNow, Name = @name where Id = @id
// note, this does not touch untracked columns
db.Products.Update(product.Id, s.Diff());
// reload
product = db.Products.Get(product.Id);
Console.WriteLine("id: {0} name: {1} desc: {2} last {3}", product.Id, product.Name, product.Description, product.LastPurchase);
// id: 1 name: Hello World desc: Nothing last 12/01/2012 5:49:34 AM
Console.WriteLine("deleted: {0}", db.Products.Delete(product.Id));
// deleted: True
Console.ReadKey();
}
}
}
@robertmircea

This comment has been minimized.

Copy link

@robertmircea robertmircea commented Mar 23, 2015

Sorry for resurrecting an old gist, but I'm having trouble with Dapper.Rainbow.Postgresql. I have the following relations:

    class DB : Database<DB>
    {
        public Table<Batch> BatchStatistics { get; set; }
        public Table<Batch> Batches { get; set; } 
    }

When I run two inserts (one for each table), the second insert generates sql which refers to Batches table instead of BatchStatistics. TableName seems to be wrong (the same as the case of the first table).

db.Batches.Insert(new {...});
db.BatchStatistics.Insert(new {...});

This is a fragment from Postgresql server log:

statement: INSERT INTO public.batch (name,...) VALUES (((E'Test')::text),...) RETURNING id
2015-03-23 22:09:12 EET LOG:  duration: 1.294 ms
2015-03-23 22:09:12 EET LOG:  statement: INSERT INTO public.batch (batch_id,pending) VALUES (((5)::int4),((0)::int4)) RETURNING id
2015-03-23 22:09:12 EET ERROR:  column "batch_id" of relation "batch" does not exist at character 27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.