How to Add Entity Data Model In Project Of MVC

How to Add Entity Data Model In Project Of MVC 

Before Add Entity Mode First You Need to Know How To Add New Project In Asp.net MVC for this Read My Privious Blog


If you Already Know All the above Define concepts  and code the Please skip and Read this blog Continue, Now Next Main objective is in this blog is how to add Data entity model in project ,with in new or in existing Project . To do Please Follow This Steps

1). First You Need To Create A Directory in to Root of Project by right click on Project and select           New item then select Add new folder Option 



2). Now A new Folder (directory ) is added into root of your project Now Write this folder new from        new folder to what you want (here i write entitymodel) of the folder


3).Now you Need to add new Data Entity model Under this entity model directory to do this you            Need to right click on directory and select Add option from menu  and then select New Item


4). Now New popup menu Open then You select Data Option from selected menu then select                    ADO.net Data Entity Model And the Write Name of Entity model Here i write AngularJs 


5). Now You Need to select Type of model content I Have Crated Database first so i choose Datafirst
      so my choise is "EF Diagram from Database" option


6).Now Click On Next button Then we Have option of selection connection string or create New            Connection string 


     If you want to use Default connect to select next option other wise click on button new                         connection 

7). Now add new connection string by Follow streps (Leave this option if You want to use Default            connection string )
     Here New Pop up Open Now Select Your Provider Type (my choise is microsoft sql server)
     and click on continue button


8).  Next step to select Sql Server and then select database and then select there mode and there                credential


    Next Step is To select Datbase from list of database contain by server (note here i am using " ."            for local server)



9) Now You Need to Test Connection required to click "text connection" button to varify valid or             non  valid connetion perameter is passes



10).  Now You Need click on ok then this connection string store in entity framework connnection              string and connection pop will closed and now click next button of entity model contnet type              pop. In the next option Show what object of sql server you want to include in you entity                      framework  


12). Now You Need to setup ModelNamespace and what kind of object you want choose by check t            the check boxex, Now You can see database entity diagrame and there relations



13).The Next Step To Use This Entity  TO Use this entity include Namespace into top of controller           using "Using " keyword Before it you need to use entity class You Get by here 


      Now Finally Write Code of Employee controller I am Write below 

using AngularDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AngularDemo.EntityModel;
namespace AngularDemo.Controllers
{
    [Authorize]
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Index()
        {
            return View();
        }
         public JsonResult GetEmployees()
        {
            using (AngularDemoEntities db = new AngularDemoEntities())
            {
                var Emplist=db.Employees;
                return Json(Emplist, JsonRequestBehavior.AllowGet);
            }
        }
        // GET: Employee/Details/5
        public ActionResult Details(int id)
        {
            using (AngularDemoEntities db = new AngularDemoEntities())
            {
                var Emp = db.Employees.Find(id);
                return Json(Emp, JsonRequestBehavior.AllowGet);
            }
        }

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

        // POST: Employee/Create
        [HttpPost]
        public ActionResult Create(Employee employee)
        {
            try
            {
                using (AngularDemoEntities db = new AngularDemoEntities())
                {
                    var Emp = db.Employees.Add(employee);
                    var s=db.SaveChanges();
                    return Json(s, JsonRequestBehavior.AllowGet);
                }
                
            }
            catch
            {
                return View(new Employee());
            }
        }

        // GET: Employee/Edit/5
        public ActionResult Edit(int id)
        {
            using (AngularDemoEntities db = new AngularDemoEntities())
            {
                var Emp = db.Employees.Find(id);
                return Json(Emp, JsonRequestBehavior.AllowGet);
            }

            
        }

        // POST: Employee/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, Employee employee)
        {
            try
            {
                using (AngularDemoEntities db = new AngularDemoEntities())
                {
                    var oldemp = db.Employees.Find(id);
                   
                    db.Entry(oldemp).CurrentValues.SetValues(employee);
                    return (db.SaveChanges() > 0) ? Json(employee, JsonRequestBehavior.AllowGet) : Json(oldemp, JsonRequestBehavior.AllowGet);
                }

               
            }
            catch
            {
                return View();
            }
        }

        // GET: Employee/Delete/5
        public ActionResult Delete(int id)
        {
            using (AngularDemoEntities db = new AngularDemoEntities())
            {
                var Emp = db.Employees.Find(id);
                return Json(Emp, JsonRequestBehavior.AllowGet);
            }
        }

        // POST: Employee/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                using (AngularDemoEntities db = new AngularDemoEntities())
                {
                    var Emp = db.Employees.Find(id);
                    db.Employees.Remove(Emp);
                    return (db.SaveChanges() > 0) ? Json(new { sucess = "true", JsonRequestBehavior.AllowGet }) : Json(new { sucess = "false", JsonRequestBehavior.AllowGet });
                }
            }
            catch
            {
                return View();
            }
        }
    }
}

       

2 comments:

  1. It's a bit superbly preserved, and my guy will continually cherish it and the recollections. web design agencies ireland

    ReplyDelete
  2. You can leave your blog page clear, as WordPress will consequently pull in each post that you add.
    https://wordpressctapro.com

    ReplyDelete

Thank You For Your Great Contribution

Featured Post

how to find n number of nodes have child or cildren

 how to find n number of nodes have child or cildren for that we use recursive function  const   data = {     'id' : '0' ,...

Popular Posts