Skip to main content

Get started with ASP.NET MVC and PetaPoco

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 asp.net mvc and dapper Micro ORM. Today I am going to explain how we can use PetaPoco in asp.net 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.
http://www.toptensoftware.com/petapoco/.

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.

AddlibraryReference

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.

PetaPocoNugetPackage

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.

SolutionExplorer

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 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

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,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
   [CustomerId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

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.

View

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.

Output

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

Shout it

kick it on DotNetKicks.com

Comments

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Popular posts from this blog

How to create Rest API(Web API) with ASP.NET Core 1.0

Recently Microsoft has released ASP.NET Core 1.0 RC2, I am getting lots of request from readers that about creating Web API. So I thought it will be a good idea to write a blog post about how to create Rest API(Web API) with ASP.NET Core 1.0.

So let’s get started via creating an ASP.NET Core 1.0 Web Application like following.



Once you click on ASP.NET Web Application, It will ask whether you need to create Web Application or Web API application. We are going to Web API so I am going to select Web API Application like following. Please note that in ASP.NET Core 1.0 there is no separate libraries or DLLs required for creating web APIs. This is just a project template.



Now once you click on OK It will create a Web API application with default values controller and program.cs.  As you know Program.cs is now starting point for the ASP.NET Core 1.0 application so It contains all the required configuration and startup items. Following is a code for that.
using System.IO; using Microsoft.Asp…

How to use migration with Entity Framework Core

Entity framework core is  the lightweight, extensible and cross-platform version of Entity Framework. Before some time, Microsoft has Released a new version of Entity Framework RC2. I have written a couple of blog post about Entity framework code first migration earlier for Entity framework 6.0. So there was a couple of request coming for writing a blog post about Entity Framework Core RC2 migration. So I thought it will be a good idea to give an overview how database migration works in Entity Framework Core RC2. This post will cover a basic scenario where we are going to create the database with existing ASP.NET Identity migration and then we are going to create a new model and have that migration applied in the database.

How to use Entity Framework Migrations: Let’s get started, To demonstrate entity framework core migrations, I am going to create a sample asp.net core web application like following.



Once we select asp.net core application it will appear the following dialog.



Now w…

Solution: gulp is not recognized as internal or external command- Visual Studio

Recently, After Upgrading to the newer version of Gulp, I was getting this error.  After doing lots of  finding online and in my system I was able to figure out that Modules like Gulp does not installed to the path. So when you try to run it from the command line it was showing below error.
gulp is not recognized as internal or external command How to solve : gulp is not recognized as internal or external command To solve this error we just need to create a new environment variable and set the NPM modules path there.

To create an environment variable,  Right click My Computer/ This Pc and click on properties.  It will open following windows.



Go to Advance System Settings and it will open system properties windows like below.



Click on Environment Variables it will open an environment and system variables windows like below.



Click on new System variable and put NODE_PATH in system variable name and %AppData%\npm\node_modules into the variable value like below.



Click on Ok. That’s it. N…