Monday, April 29, 2013

Asp.net MVC Scaffolding in code first scenarios


In this article we will walk through the creation of a read/write/update/delete of Recipes and its related (many) Ingredients using MvcScaffolding and MVC 4.

MVC Scaffolding gives you a fast way to get some helpful, effective code, that you can then edit and adapt according to your needs.


In other words, MvcScaffolding  can create automatically:
1. Controllers for creating, viewing, updating and deleting instances of your model,
2. Corresponding Views for creating, viewing, updating, and deleting data,
3. Database contexts from your model,
4. Catalyze the creation of a database model including support for one-to-many relationships and more…

In this article we'll create a blood donar application using MVC Scaffolding and code first.

Let’s create two models for blood group and donar. Add the following classes to our Models folder, then compile the solution :

BloodGroup.cs

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Blood_Donar.Models
{
 public class bloodgroup
{
public int bloodgroupid { get; set; }

[DisplayName ("Blood Group")]
public string name { get; set; }

}
}


Donar.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Blood_Donar.Models
{
public class donar
{
public int ID { get; set; }

[DisplayName ("Donar Name")]
[Required(ErrorMessage="Name is required")]
public string Name { get; set; }

public string Address { get; set; }

[Required(ErrorMessage="Mobile no is required.")]
public int Mobile { get; set; }

[DisplayName ("Blood Group")]
public int bloodgroupid { get; set; }

public virtual bloodgroup bloodgroups { get; set; }

}
}

Execute following command into Package Manager Console:
1. Install-Package EntityFramework
2. Install-Package mvcscaffolding

Next, we will create a complete Create-Read-Update-Delete (CRUD) UI for those models by executing a single command into Package Manager Console:



Scaffold Controller Donar

This command will create a database context,  a controller, and all the views for Create-Read-Update-Delete (CRUD) actions. Right now you can run it (Shift-F5).

If you have SQL Server Express running on your machine, EFCodeFirst will automatically connect to it, create your database schema, and you’ve got a basic working application without writing a single line of procedural code. Also you can add a SQL Server Compact to your project by issuing the following command in the Package Manager Console:

Install-Package EFCodeFirst.SqlServerCompact

Again, execute following command for BloodGroup model

Scaffold Controller BloodGroup

To create repositary class execute following command into package manager console

Scaffold Controller Donar –Repository -Force

Now the scaffolder will produce two additional class, Donar Repository, and the following interface which Donar Repository implements:

public interface IdonarRepository
{
IQueryable<donar> All { get; }
IQueryable<donar> AllIncluding(params Expression<Func<donar, object>>[] includeProperties);
donar Find(int id);
void InsertOrUpdate(donar donar);
void Delete(int id);
void Save();
}

Follow same process for Bloodgroup.

Now run the application and see the magic of MVCScaffolding.

0 comments:

Post a Comment