User Id :    Password :      New Member   Forgot Password  
Get started with ASP.NET MVC and PetaPoco
Description Get started with ASP.NET MVC and PetaPoco.   No. of Views     2937
  Rating     0
Author Jalpesh Vadgama   Posted On     21 Jun 2011

Sample Code   Download Code

Micro ORM are having their pro and cons and right now its a buzz in the Microsoft.NET developers buzz. I personally like a Micro ORM Concept and I already posted some blog posts with mvc and dapper Micro ORM. Today I am going to explain how we can use PetaPoco in mvc application.  First lets get little bit introduction about PetaPoco.

PetaPoco is developed by Top Ten software and It’s Micro ORM inspired by Masive and Dapper. Following is link where you can get more information about that.


To install PetaPoco in our application first we need to add a library reference as Its already available there. so Let’s add library reference like following.



Once click on add library reference a dialog box appears to find the the library package like following. I have written PetaPoco in search box and it will be appear in list like following.



Select first one then click Install and It will install the PetaPoco in your application. Once installation done you will have one t4 teamplte and one PetaPoco.cs file in your model section of ASP.NET application like following.



Now we are ready use the PetaPoco in our application. Now let’s use same DB which we have use for dapper demonstration let’s take same customer table like following.Following is a script to create table

CREATE TABLE [dbo].[Customer](
    [CustomerId] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](50) NULL,
    [LastName] [nvarchar](50) NULL,
    [Address] [nvarchar](256) NULL,
    [City] [nvarchar](50) NULL,
    [CustomerId] ASC


Now I have inserted some sample data in the table and now its ready to use. Now I have added a Model Entity class which contains same properties as Table columns. Just like following.


namespace CodeSimplified.Models
    public class Customer
        public int CustomerId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string City { get; set; }


Now we are ready with our class I have create a new class called CustomerDB to fetch customer from the database. In that class I have created method called GetCustomers which will fetch all the customer from database. Here I have created a database object of PetaPoco and then I have using Its query method to query database. Following is a code for that.


using System.Collections.Generic;
namespace CodeSimplified.Models
    public class CustomerDB
        public IEnumerable<Customer> GetCustomers()
            var databaseContext = new PetaPoco.Database("MyConnectionString");
            return databaseContext.Query<Customer>("Select * from Customer");


Now as this is for demo purpose only lets create a method in Home Controller class to get customers like following.


using System.Web.Mvc;
namespace CodeSimplified.Controllers
    public class HomeController : Controller
        public ActionResult Index()
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            return View();
        public ActionResult About()
            return View();
        public ActionResult Customer()
            var customerDb = new Models.CustomerDB();
            return View(customerDb.GetCustomers());


Now once done we with Customer Method in Home Controller let’s create view for that. Here I have created a strongly typed view like following for customers.



Now we are done with our view also So let’s run the project and let’s see output in browser. You can see output in browser as expected like following.




So it’s very easy to use PetaPoco with ASP.NET MVC. Hope you liked it. Stay tuned for more..


About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at Jalpesh Vadgama
No Photo
Country India
Company N/A
Home Page

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Write your comment here.
Verification Code