0
how do I execute an SQL query with ASP MVC5 + Entity?
Example, I need to use "Count" in a table called "templates" am dev php and am learning now Asp.net framework mvc 5 and have no idea how to do this...
To create the MVC I used the option of Entity itself (I created the model, context) ai already generated the controller and the views
Follows code from 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;
namespace TeZ.Models
{
public class MembersController : Controller
{
private MembersContext db = new MembersContext();
// GET: Members
public ActionResult Index()
{
return View(db.members.ToList());
}
// GET: Members/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Member member = db.members.Find(id);
if (member == null)
{
return HttpNotFound();
}
return View(member);
}
// GET: Members/Create
public ActionResult Create()
{
return View();
}
// POST: Members/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "mbMatricula,mbName,mbFunc,mbMail,mbPic,mbPass,mbActive")] Member member)
{
if (ModelState.IsValid)
{
db.members.Add(member);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(member);
}
// GET: Members/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Member member = db.members.Find(id);
if (member == null)
{
return HttpNotFound();
}
return View(member);
}
// POST: Members/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "mbMatricula,mbName,mbFunc,mbMail,mbPic,mbPass,mbActive")] Member member)
{
if (ModelState.IsValid)
{
db.Entry(member).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(member);
}
// GET: Members/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Member member = db.members.Find(id);
if (member == null)
{
return HttpNotFound();
}
return View(member);
}
// POST: Members/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Member member = db.members.Find(id);
db.members.Remove(member);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
if you have the code of any
Controller
?– novic
I have yes I will be editing and putting
– Wallace Bruno
private MembersContext db = new MembersContext();
this is your correct ORM class so it would bedb.modelos.count()
....– novic
However much the
Enumerable.Count()
resolve, you can make raw SQL query using the .Database.SqlQuery("SELECT * ...")
if the example was hypothetical and you want to do another query.– Gabriel Coletta
But then, I put this data where? In the controller before returning to the view? And what do I call the data in the view? I’m pretty dumb about Asp and Entity use Laravel hehe a lot
– Wallace Bruno
Thanks a lot for the help, I already got =) only use @Model.Count() inside the Tks view
– Wallace Bruno