Entity Framework Code First Approach in MVC with example Step by step tutorial

In Code first approach we create POCO (Plain Old CLR Objects) is the ability to add and use your own custom data classes along with your data model. It’s also known as persistence-ignorant objects) classes and then we create the database for this POCO classes.

Let’s see the steps to configure and add Entity Framework and also how to connect it with the database.


Database:-


Step1:-First we create a project with the name of Code-First-Approach




Step2:- After creating application, now we will Install Entity Framework in MVC Application. We click on right on our MVC application and select the “Manage NuGet Packages” and search “Entityframework” and installed them





Step3:- After installation “Entityframework packges ” , we will create the POCO class. For this we click on right key of model and choose a .cs file. Here we create with name of “record”

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Code_First_Approach.Models
{
    public class record
    {
        [Key]
        public string record_id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Email { get; set; }
    }
}

Step4:- Now we create a connection string in web. config file
<connectionStrings>
    <add name="constr" providerName="System.Data.SqlClient" connectionString="Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678;  App=EntityFramework" />
  </connectionStrings>

Step5:- Now we create a DbContext class .we again click on right key on model and add a .cs file .here we choose the name of RecordContext

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Code_First_Approach.Models
{
    public class RecordContext : DbContext
    {
        public RecordContext() : base("name=constr") { }

        public DbSet <record> Students { get; set; }
    }
}
Step6:- We will add controller with name of HomeController


using Code_First_Approach.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;

using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Code_First_Approach.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            //if you are getting context has changed since the database was created error then uncomment below line but i will remove your database so be careful
            //   Database.SetInitializer(new DropCreateDatabaseIfModelChanges<RecordContext>());
            return GetDetails();
        }
        public ActionResult GetDetails()
        {
            var model = new List<record>();
            try
            {

                using (var context = new RecordContext())
                {
                    var value = context.Students.ToList();
                    foreach (var item in value)
                    {
                        var stdModel = new record();
                        stdModel.Name = item.Name;
                        stdModel.Age = item.Age;
                        stdModel.Email = item.Email;
                        stdModel.record_id = item.record_id;
                       
                        model.Add(stdModel);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            //return PartialView("_index", model);
            return View("Index", model);
           
        }
    }

}

Step6:- We will add View with name of Index.cshtml
@model List<Code_First_Approach.Models.record>


@{
    Layout = null;
   
           
}
<h1>Informations</h1>
@if (Model != null)
{
    <div>
        <table cellspacing="0" width="50%" border="1">
            <thead>
                <tr>
                    <th>
                      Record  ID
                    </th>
                    <th>
                         Name
                    </th>
                    <th>
                        Age
                    </th>
                    <th>
                        Email
                    </th>
                 
                   
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td align="center">
                            @item.record_id
                        </td>
                        <td align="center">
                            @item.Name
                        </td>
                        <td align="center">
                            @item.Age
                        </td>
                        <td align="center">
                            @item.Email
                        </td>
                     
                      
                    </tr>
                }
            </tbody>
        </table>
    </div>
}


Out-Put:-


Entity Framework Code First Approach in MVC with example Step by step tutorial Entity Framework Code First Approach in MVC with example Step by step tutorial Reviewed by NEERAJ SRIVASTAVA on 11:27:00 PM Rating: 5

No comments:

Powered by Blogger.