Step 1 - Create new ASP.NET MVC Empty project, using VS 2013.
Step 2 - Add new project by right clicking on project, adding class library, and giving name to it.
Step 3 - Create one database using SQL Server with below script.
- CREATE TABLE [dbo].[UserDetails](
- [Id] [int] IDENTITY(1,1) NOT NULL,
- [Name] [varchar](50) NULL,
- [Address] [varchar](50) NULL,
- [City] [varchar](50) NULL,
- CONSTRAINT [PK_UserDetails] PRIMARY KEY CLUSTERED
- (
- [Id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
Step 4 - Now, create EDMX from our Database. Just right click on Infrastructure, add New Item => Select on ADO.NET Entity Data Model from data template, and give name to it. Click on Next.
Select Generate from Database, click on Next button. Now, give New connection string and select your database, Click OK.
It will automatically take entity name. Click on Next and select tables to include the database object in your Model. Then, click on Finish. You will see the edmx as,
Step 5 - Also, add one new class library for Repository pattern. Add reference of Infrastructure dll and add Entity Framework from NuGet package.
Step 6 - Now, create GenericRepository, IGenericRepository, UnitOfWork classes and add appropriate codes to it, as follows.
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Repository
- {
- public interface IGenericRepository<T> where T : class
- {
- IEnumerable<T> GetAll(Expression<Func<T, bool>> filter = null,
- Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
- params Expression<Func<T, object>>[] np);
-
- T GetByID(object id);
-
- void Insert(T e);
-
- void Delete(object id);
-
- void Delete(T eDlt);
-
- void Update(T eUpdt);
-
- void Save();
-
- T GetSingle(Expression<Func<T, bool>> where, params Expression<Func<T, object>>[] np);
- }
- }
Now, add the below code into a UnitOfWork class.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Infrastructure;
- using Repository.GenericRepository;
-
- namespace Repository.GenericRepository
- {
- public class UnitOfWork : IDisposable
- {
- private CRUD_OperationEntities context = new CRUD_OperationEntities();
- private IGenericRepository<UserDetail> userRepository;
- public IGenericRepository<UserDetail> UserRepository
- {
- get
- {
- return userRepository ?? (userRepository = new GenericRepository<UserDetail>(context));
- }
- }
- }
- }
Step 7 - Add connection string in App config of Repository class library as well as in web config file of our main project.
- <connectionStrings>
- <add name="CRUD_OperationEntities" connectionString="metadata=res://*/CRUDOperation.csdl|res://*/CRUDOperation.ssdl|res://*/CRUDOperation.msl;provider=System.Data.SqlClient;provider connection string="data source=RUPESH-PC\SA;initial catalog=CRUD_Operation;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
- </connectionStrings>
Step 8 - Now, create one Home Controller in application. First, we will create one IndexViewModel by adding properties and constructor in it.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace CRUD_Using_Repository_Pattern.ViewModel
- {
- public class IndexViewModel
- {
- private Infrastructure.UserDetail x;
-
- public IndexViewModel()
- { }
-
- public IndexViewModel(Infrastructure.UserDetail user)
- {
- Id = user.Id;
- Name = user.Name;
- Address = user.Address;
- City = user.City;
- }
-
- public int Id { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public string City { get; set; }
- }
- }
Add the below code in Home Controller to perform the CRUD operation.
Create Partial View _Add for Add action method as,
- @model CRUD_Using_Repository_Pattern.ViewModel.IndexViewModel
-
- @using (Html.BeginForm("Create", "Home", FormMethod.Post))
- {
- <div style="margin-left:15%">
- <div>
- @Html.LabelFor(model => model.Name)
- @Html.EditorFor(model => model.Name)
- </div>
- <br />
- <div>
- @Html.LabelFor(model => model.Address)
- @Html.EditorFor(model => model.Address)
- </div>
- <br />
- <div>
- @Html.LabelFor(model => model.City)
- @Html.EditorFor(model => model.City)
- </div>
- <br />
- <input type="submit" value="Add" />
- </div>
- }
Create Partial View _Edit for Edit Action as,
- @model CRUD_Using_Repository_Pattern.ViewModel.IndexViewModel
-
- @using (Html.BeginForm("Update", "Home", FormMethod.Post))
- {
- <div style="margin-left:15%">
- <div>
- @Html.LabelFor(model => model.Id)
- @Html.TextBoxFor(model => model.Id, new { @readonly = "readonly" })
- </div>
- <br />
- <div>
- @Html.LabelFor(model => model.Name)
- @Html.EditorFor(model => model.Name)
- </div>
- <br />
- <div>
- @Html.LabelFor(model => model.Address)
- @Html.EditorFor(model => model.Address)
- </div>
- <br />
- <div>
- @Html.LabelFor(model => model.City)
- @Html.EditorFor(model => model.City)
- </div>
- <br />
- <input type="submit" value="Update" />
- </div>
- }
Now, if we run the application, you will see the list of all users.
Now, click on Add New to add new record.
After adding new record, click on edit of Jeetendra to update surname.
After updating the record, you will see.
Now, click on delete the record of Amit K,
After deleting the record, our list of all users will be as shown below.
Summary - This article will help fresher candidates to understand CRUD operations using Repository Pattern, UnitOfWork.