crud entity framework code example
Example 1: crud operation without entity framework in mvc
using System.Web.Mvc;using CRUDinMVC.Models; namespace CRUDinMVC.Controllers{ public class StudentController : Controller { // 1. *************RETRIEVE ALL STUDENT DETAILS ****************** // GET: Student public ActionResult Index() { StudentDBHandle dbhandle = new StudentDBHandle(); ModelState.Clear(); return View(dbhandle.GetStudent()); } // 2. *************ADD NEW STUDENT ****************** // GET: Student/Create public ActionResult Create() { return View(); } // POST: Student/Create [HttpPost] public ActionResult Create(StudentModel smodel) { try { if (ModelState.IsValid) { StudentDBHandle sdb = new StudentDBHandle(); if (sdb.AddStudent(smodel)) { ViewBag.Message = "Student Details Added Successfully"; ModelState.Clear(); } } return View(); } catch { return View(); } } // 3. ************* EDIT STUDENT DETAILS ****************** // GET: Student/Edit/5 public ActionResult Edit(int id) { StudentDBHandle sdb = new StudentDBHandle(); return View(sdb.GetStudent().Find(smodel => smodel.Id == id)); } // POST: Student/Edit/5 [HttpPost] public ActionResult Edit(int id, StudentModel smodel) { try { StudentDBHandle sdb = new StudentDBHandle(); sdb.UpdateDetails(smodel); return RedirectToAction("Index"); } catch { return View(); } } // 4. ************* DELETE STUDENT DETAILS ****************** // GET: Student/Delete/5 public ActionResult Delete(int id) { try { StudentDBHandle sdb = new StudentDBHandle(); if (sdb.DeleteStudent(id)) { ViewBag.AlertMsg = "Student Deleted Successfully"; } return RedirectToAction("Index"); } catch { return View(); } } }}
Example 2: CRUD entity framework
using System;
using System.Linq;
namespace EFGetStarted
{
internal class Program
{
private static void Main()
{
using (var db = new BloggingContext())
{
// Note: This sample requires the database to be created before running.
// Create
Console.WriteLine("Inserting a new blog");
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.SaveChanges();
// Read
Console.WriteLine("Querying for a blog");
var blog = db.Blogs
.OrderBy(b => b.BlogId)
.First();
// Update
Console.WriteLine("Updating the blog and adding a post");
blog.Url = "https://devblogs.microsoft.com/dotnet";
blog.Posts.Add(
new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" });
db.SaveChanges();
// Delete
Console.WriteLine("Delete the blog");
db.Remove(blog);
db.SaveChanges();
}
}
}
}
Example 3: crud operation without entity framework in mvc
<connectionStrings> <add name="StudentConn" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True;Pooling=False"/> </connectionStrings>
Example 4: crud operation without entity framework in mvc
Create procedure [dbo].[UpdateStudentDetails] ( @StdId int, @Name nvarchar (50), @City nvarchar (50), @Address nvarchar (100) ) as begin Update StudentReg set Name=@Name, City=@City, Address=@Address where Id=@StdId End
Example 5: crud operation without entity framework in mvc
Create procedure [dbo].[DeleteStudent] ( @StdId int ) as begin Delete from StudentReg where Id=@StdId End