How to use EF Repository pattern to do database operation like Get,Insert,Update,Delete in Asp.Net MVC.
1> Install Unity.MVC5 in Package Manager Console
Goto --> Tools-->NuGet Package Manager --> Package Manager Console -->
PM> Install-Package Unity.Mvc5
and Hit the Enter.
2> Now Create a Interface name as IRepository.cs .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ItsMe
{
//this generic interface repository for insert update delete and get operation.
public interface IRepository<TEnt, in TPk> where TEnt : class
{
/// <summary>
/// Get All Data
/// </summary>
/// <returns>IRepository</returns>
IEnumerable<TEnt> Getll();
/// <summary>
/// Get Element by Id
/// </summary>
/// <param name="id">IRepository</param>
/// <returns></returns>
TEnt GetById(TPk id);
/// <summary>
/// Insert the Data
/// </summary>
/// <param name="entity">IRepository</param>
void Insert(TEnt entity);
/// <summary>
/// Update the Data
/// </summary>
/// <param name="entity">IRepository</param>
void Update(TEnt entity);
/// <summary>
/// Delete the data
/// </summary>
/// <param name="entity">IRepository</param>
void Delete(TEnt entity);
}
}
3> Now we create a class , so that IRepository.cs get Implemented.
>first we have to create a table and name as Types
>Field are:- Id, Name & Status.
>Create Model for Type table.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace ItsMe.Areas.Admin.Models
{
[Table("Type")]
public class Types
{
public int Id { get; set; }
public string Name { get; set; }
public bool Status { get; set; }
}
}
>Map the modu;e with Dbset<Type> in Context Class
>Create a class name As TyepeService.
IRepository<Types, int> here Types is model name.
using ItsMe;
using ItsMe.Models;
using ItsMe.Areas.Admin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace ItsMe.Services
{
public class TypeService : IRepository<Types, int>
{
DefaultConnection context = new DefaultConnection();
public void Delete(Types entity)
{
context.typeContext.Remove(entity);
}
public Types GetById(int id)
{
return context.typeContext.Find(id);
}
public IEnumerable<Types> GetAll()
{
return context.typeContext.ToList();
}
public void Insert(Types entity)
{
context.typeContext.Add(entity);
}
public void Update(Types entity)
{
context.Entry(entity).State = EntityState.Modified;
context.SaveChanges();
}
}
}
In my next session i will show you how to use Repository method in Controller.
3> Now we create a class , so that IRepository.cs get Implemented.
>first we have to create a table and name as Types
>Field are:- Id, Name & Status.
>Create Model for Type table.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace ItsMe.Areas.Admin.Models
{
[Table("Type")]
public class Types
{
public int Id { get; set; }
public string Name { get; set; }
public bool Status { get; set; }
}
}
>Map the modu;e with Dbset<Type> in Context Class
>Create a class name As TyepeService.
IRepository<Types, int> here Types is model name.
using ItsMe;
using ItsMe.Models;
using ItsMe.Areas.Admin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace ItsMe.Services
{
public class TypeService : IRepository<Types, int>
{
DefaultConnection context = new DefaultConnection();
public void Delete(Types entity)
{
context.typeContext.Remove(entity);
}
public Types GetById(int id)
{
return context.typeContext.Find(id);
}
public IEnumerable<Types> GetAll()
{
return context.typeContext.ToList();
}
public void Insert(Types entity)
{
context.typeContext.Add(entity);
}
public void Update(Types entity)
{
context.Entry(entity).State = EntityState.Modified;
context.SaveChanges();
}
}
}
In my next session i will show you how to use Repository method in Controller.