Work with ASP.NET MVC and PetaPoco

No.of Views3164
Bookmarked0 times
By  jalpesh   On  02 Jul 2011 11:07:22
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 articles with mvc and dapper Micro ORM.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at



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 articles 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.


Image Loading

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.

Image Loading

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.

Image Loading

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.

/****** Object:  Table [dbo].[Customer]    Script Date: 06/17/2011 02:27:34 ******/
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.

Image Loading

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.

Image Loading

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

Sign Up to vote for this article
About Author
Occupation-Software Engineer
Member Type-Gold
Joined date-08 May 2010
Home Page-
Blog Page-
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
Other popularSectionarticles
There is no comments for this articles.
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top