I have created crud functions using webApi and MVC using an Entity framework, but, I have to do the same thing without using an entity framework which i am not able to do it. I am beginner here, can anybody help me with that? I really need to fix that
What I have tried:
I have created Model class called Abc, Repository, and Api contoller, Controller for Mvc, I have used "Abc" table for Entity framework and diagram.
Class in Repository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebserviceDemo.Models;
using WebserviceDemo.Interface;
namespace WebserviceDemo.Repositories
{
public class AbcRepository : IAbcRepository
{
demoradiologyEntities ProductDB = new demoradiologyEntities();
public IEnumerable<abc> GetAll()
{
return ProductDB.Abcs;
}
public Abc Get(int id)
{
return ProductDB.Abcs.Find(id);
}
public Abc Add(Abc item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
ProductDB.Abcs.Add(item);
ProductDB.SaveChanges();
return item;
}
public bool Update(Abc item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
var products = ProductDB.Abcs.Single(a => a.ID == item.ID);
products.Abr = item.Abr;
products.Name = item.Name;
ProductDB.SaveChanges();
return true;
}
public bool Delete(int id)
{
Abc products = ProductDB.Abcs.Find(id);
ProductDB.Abcs.Remove(products);
ProductDB.SaveChanges();
return true;
}
}
}
Interface:
using WebserviceDemo.Models;
namespace WebserviceDemo.Interface
{
interface IAbcRepository
{
IEnumerable<abc> GetAll();
Abc Get(int id);
Abc Add(Abc item);
bool Update(Abc item);
bool Delete(int id);
}
}
Api controller
namespace WebserviceDemo.Controllers
{
public class AbcController : ApiController
{
static readonly IAbcRepository repository = new AbcRepository();
public IEnumerable<abc> GetAllAbc()
{
return repository.GetAll();
}
public Abc PostAbc(Abc item)
{
return repository.Add(item);
}
public IEnumerable<abc> PutAbc(int id, Abc product)
{
product.ID = id;
if (repository.Update(product))
{
return repository.GetAll();
}
else
{
return null;
}
}
public bool DeleteAbc(int id)
{
if (repository.Delete(id))
{
return true;
}
else
{
return false;
}
}
}
}
Controller
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebserviceDemo.Models;
using WebserviceDemo.Interface;
using WebserviceDemo.Repositories;
namespace WebserviceDemo.Controllers
{
public class BcdController : Controller
{
public demoradiologyEntities db = new demoradiologyEntities();
public ActionResult Index()
{
return View(db.Abcs.ToList());
}
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Abc abc = db.Abcs.Find(id);
if (abc == null)
{
return HttpNotFound();
}
return View(abc);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="ID,Abr,Name")] Abc abc)
{
if (ModelState.IsValid)
{
db.Abcs.Add(abc);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(abc);
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Abc abc = db.Abcs.Find(id);
if (abc == null)
{
return HttpNotFound();
}
return View(abc);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="ID,Abr,Name")] Abc abc)
{
if (ModelState.IsValid)
{
db.Entry(abc).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(abc);
}
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Abc abc = db.Abcs.Find(id);
if (abc == null)
{
return HttpNotFound();
}
return View(abc);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Abc abc = db.Abcs.Find(id);
db.Abcs.Remove(abc);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}