Entity Framework Database First Approach In MVC with example Step by step tutorial

In this approach, we create model codes likes classes, properties, DBContext etc. from the database in the project and those classes become the link between the database and controller.
Let’s see the steps to configure and add Entity Framework and also how to connect it with the database.
Step1: -First we create a project with the name of DatabaseFirstApproach


Step2:-
Click on left-hand side server explorer and we add a connection for database

Step3: -We choose a data source (Microsoft sql server)

Step4: -We make a database connection (connection string) and then click Test Connection to make sure all settings are correct.


Step 5: -We get all tables from database in our server explorer

Step6: -Now we choose the model and click the right button of mouse and add new item then we Add ADO.NET Entity Data Model

Step7: -Then the Entity Data Model Wizard will open up where we select EF Designer database option.

Step8: - The Connection String is generated

Step9: - Now we need to choose the Tables to connect and work with Entity Framework. Here we select record table

Controller
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the values from the record Table
In Index Action method, we fetch the values from record table and returned to the View.
Controller
using DatabaseFirstApproach.Models;
using System.Linq;
using System.Web.Mvc;


namespace DatabaseFirstApproach.Controllers
{
    public class HomeController : Controller
    {
     
         public ActionResult Index()
        {
            CodeSolutionEntities entities = new CodeSolutionEntities();
            return View(from record in entities.records
                        select record);
        }
    }
}
View
We click on right click and add view




CodeSolutionEntities is declared as IEnumerable which specifies that it will be available as a Collection.
Index view
@model IEnumerable<DatabaseFirstApproach.Models.record>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>

<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }

        th, td {
            text-align: left;
            padding: 8px;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        th {
            background-color: #4CAF50;
            color: white;
        }
    </style>

</head>
<body>
    <h1>Entity Framework Database First Approach In MVC with example Step by step tutorial </h1>
    <hr />
    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Contact Number</th>
            <th>Email</th>

        </tr>
        @foreach (DatabaseFirstApproach.Models.record data in Model)
        {
            <tr>
                <td>@data.record_id</td>
                <td>@data.Name</td>
                <td>@data.Contact_no</td>
                <td>@data.Email</td>

            </tr>
        }
    </table>
</body>

</html>

Out-Put:-


Entity Framework Database First Approach In MVC with example Step by step tutorial Entity Framework Database First Approach In MVC with example Step by step tutorial Reviewed by NEERAJ SRIVASTAVA on 11:01:00 PM Rating: 5

No comments:

Powered by Blogger.