Skip to content

Quick start

Wade Baglin edited this page Dec 15, 2015 · 9 revisions

Define your entities

public class Article
{
    public long Id { get; set; }
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
    public bool Draft { get; set; }
    public string Content { get; set; }
}

For more information about mapping entities, see mapping

Create an instance of PetaPoco

var db = new PetaPoco.Database("connectionStringName");

For more information about instantiating PetaPoco, see ways to instantiate PetaPoco

Save an entity

var article = new Article { Title = "Our first entry", DateCreated = DateTime.UtcNow(), Content = "Some content..." };

db.Save("Articles", "Id", article);
// With more advanced mapping, you could have called db.Save(article);

For more information about inserting, updating or saving entities, see inserting, updating and saving

Query for all articles

foreach (var a in db.Query<article>("SELECT * FROM Articles"))
{
    Console.WriteLine($"{a.Id} - {a.Title}");
}

For more information about querying, see querying

Delete the article

db.Delete("Articles", "Id", article.Id);
// Or db.Delete("Articles", "Id", article);
// With more advanced mapping, you could have called db.Delete(article);