Create Simple MVC Application Using Entity- Framework Database First Model

To create a simple MVC application use the following procedure.

I have created a simple MVC application using Visual Studio 2010 and .Net Framework 4.0 and SQL Server 2008 using
the following:

  1. Open Visual Studio 2010, select "File" -> "New" -> "Project...". The New Project Window will open. Select the ASP.NET MVC3 Web Application template, change the Name, change the Location if required, then select "OK". The New ASP.NET MVC3 Project Window will open. Select "Empty template" then press the "OK" button.
  2. Open SQL Server, create a database named "Test", create a table named "employee" with columns such as Code as Numeric, ID as string, Name as string and Department as string.
  3. After creating the database, go to the Solution Explorer of Visual Studio and right-click on the model folder. Then go to "Add" -> "New Item". The Add New Item Window will open, select ADO.NET Entity Data Model. Rename the entity data model to test, than press "Ok".
  4. Select "Generate from database" from the Entity Data Model Wizard Window and press "Next", select an existing connection to create a new connection. Then press the "Next" button.
  5. Select the table or tables, if required rename the model name, then press "Ok"; the selected table will appear in the test.edmx file.
  6. Again go to the Solution Explorer, right-click on the Controllers folder, click "Add Controller". In the Add Controller window rename the controller to "Empcontroler", select the option "Add action for Create, Update, Delete and Detail Scenario". Then press "Add".
  7. The EmpControler class will appear in the Visual Studio. Create the object of the entity class that you created when you created the model. The code is given below.
  8. Build the application, so the employee model class will appear while creating the view for your application.

    //Create the object of testentities class
    Models.testEntities TE = new Models.testEntities();
     
  9. Then right-click on the index function and select "Add View", rename that view if required. Select "Create a Strongly-typed View" than select "Employee Model", then select Scaffold template as to list. Than press "Add".
  10. After creating the view write the following code in the index function:

    public ActionResult Index()
    {
       //Querying to the employee table
       List<Models.Employee> emp = TE.Employees.ToList();
       //Passing employee list to view as a paramenter
       return View(emp);
    }
     
  11. Again right-click on the Detail function and click "Add view", select scaffold as to Details. In the Details function write the following code:

    public
    ActionResult Details(int id)
    {
       Models.Employee emp = TE.Employees.Where(x => x.Code == id).Single();
       return View(emp);
    }
     
  12. In the post create function, like in the preceding steps to create the view, select the scaffold to create and write the following code:

    [HttpPost]
    public ActionResult Create(Models.Employee emp)
    {
        try
       
    {
            TE.Connection.Open();
            TE.Employees.AddObject(emp);
            TE.SaveChanges();
            TE.AcceptAllChanges();
            return RedirectToAction("Index");
        }
        catch
       
    {
            return View();
        }
    }

     

  13. Create a view to edit and write the following code:

    public ActionResult Edit(int id)
    {
        TE.Connection.Open();
        //Querying to the employee table
        Models.Employee emp = TE.Employees.Where(x => x.Code == id).Single();
        return View(emp);
    }
    // POST: /Emp/Edit/5
    [HttpPost]
    public ActionResult Edit(int id, Models.Employee emp)
    {
        Try
        {
               // TODO: Add update logic here
               Models.Employee emp1 = TE.Employees.Where(x => x.Code == id).Single();
               emp1.Name=emp.Name;
               emp1.Department=emp.Department;
               TE.AcceptAllChanges();
               TE.SaveChanges();
               return RedirectToAction("Index");
        }
        Catch
        {
              return View();
        }
    }
     
  14. Create the view for the delete and write the following code:

    public ActionResult Delete(int id)
    {
         Models.Employee emp = TE.Employees.Where(x => x.Code == id).Single();
         return View(emp);
    }
    // POST: /Emp/Delete/5
    [HttpPost]
    public ActionResult Delete(int id, FormCollection collection)
    {
        try
        {
            
    // TODO: Add delete logic here
             Models.Employee emp = TE.Employees.Where(x => x.Code == id).Single();
             TE.Employees.DeleteObject(emp);
             TE.SaveChanges();
             TE.AcceptAllChanges();
             return RedirectToAction("Index");
        }
        Catch
        {
            return View();
        }
    }

Run the application and do a create, update and delete of your data. Please feel free to ask any questions.

Up Next
    Ebook Download
    View all
    Learn
    View all