Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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()
        {
            // TO DO : Code to get the list of all the records in database
            return ProductDB.Abcs;
        }

        public Abc Get(int id)
        {
            // TO DO : Code to find a record in database
            return ProductDB.Abcs.Find(id);
        }

        public Abc Add(Abc item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            // TO DO : Code to save record into database
            ProductDB.Abcs.Add(item);
            ProductDB.SaveChanges();
            return item;
        }

        public bool Update(Abc item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            // TO DO : Code to update record into database
            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)
        {
            // TO DO : Code to remove the records from database
            Abc products = ProductDB.Abcs.Find(id);
            ProductDB.Abcs.Remove(products);
            ProductDB.SaveChanges();
            return true;
        }
    }
}


Interface:
C#
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
C#
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

C#
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();

        // GET: /Bcd/
        public ActionResult Index()
        {
            return View(db.Abcs.ToList());
        }

        // GET: /Bcd/Details/5
        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);
        }

        // GET: /Bcd/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Bcd/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [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);
        }

        // GET: /Bcd/Edit/5
        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);
        }

        // POST: /Bcd/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [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);
        }

        // GET: /Bcd/Delete/5
        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);
        }

        // POST: /Bcd/Delete/5
        [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);
        }
    }
}
Posted
Updated 19-May-16 7:23am
v2
Comments
F-ES Sitecore 19-May-16 11:07am    
What do you plan on using instead? If it is ado.net (probably your best solution for now) then google "ado.net getting started" and you'll find code samples that show how you select from tables, insert data etc. You can either write your own SQL (something that EF does for you) to execute, or you can create Stored Procedures that do the work and make your ado.net commands call those stored procedures.
Upadev 23-May-16 9:45am    
Hi,Thanks for the help.I am trying to write sql query to connect it to its controller to add,update,delete data and api controller to get data in JSON and XML for crud functions.

1 solution

You could use DbCommandBuilder:
C#
using (DbCommand cmd = conn.CreateCommand())
{
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from " + tableName;
    cmd.CommandTimeout = 10;
    using (DbDataAdapter da = factory.CreateDataAdapter())
    {
        DbCommandBuilder cb = factory.CreateCommandBuilder();
        da.SelectCommand = cmd;
        DataTable dt = new DataTable();
        da.FillSchema(dt, SchemaType.Source);
        cb.DataAdapter = da;
        DbCommand[] cmds = new DbCommand[3]
        cmds[0] = cb.GetUpdateCommand();
        cmds[1] = cb.GetDeleteCommand();
        cmds[2] = cb.GetInsertCommand();
    }
}

See this article on CodeProject for more information: Don't hard code your DataProviders[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900