{"id":4567,"date":"2023-02-17T11:52:48","date_gmt":"2023-02-17T11:52:48","guid":{"rendered":"https:\/\/isophal.com\/?p=4567"},"modified":"2023-06-22T05:05:37","modified_gmt":"2023-06-22T05:05:37","slug":"%e1%9e%9f%e1%9f%92%e1%9e%9c%e1%9f%82%e1%9e%84%e1%9e%99%e1%9e%9b%e1%9f%8b%e1%9e%a2%e1%9f%86%e1%9e%96%e1%9e%b8-entity-framework","status":"publish","type":"post","link":"https:\/\/isophal.com\/news\/2023\/02\/17\/4567.html\/","title":{"rendered":"\u179f\u17d2\u179c\u17c2\u1784\u1799\u179b\u17cb\u17a2\u17c6\u1796\u17b8 Entity Framework"},"content":{"rendered":"\n<p>\u1796\u17b8\u1798\u17bb\u1793\u17a1\u17be\u1799 .Net 3.5 \u1780\u17d2\u1793\u17bb\u1784\u1793\u17b6\u1798\u1787\u17b6\u17a2\u17d2\u1793\u1780\u17a2\u1797\u17b7\u179c\u178c\u17d2\u178d\u1793\u17cd\u1799\u17be\u1784\u1785\u17d2\u179a\u17be\u1793\u178f\u17c2\u1794\u17d2\u179a\u17be\u178a\u17be\u1798\u17d2\u1794\u17b8\u179f\u179a\u179f\u17c1\u179a\u1780\u17bc\u178a ADO.NET \u178a\u17be\u1798\u17d2\u1794\u17b8\u17a2\u1793\u17bb\u179c\u178f\u17d2\u178f\u1794\u17d2\u179a\u178f\u17b7\u1794\u178f\u17d2\u178f\u17b7\u1780\u17b6\u179a &nbsp;Perform CRUD \u1787\u17b6\u1798\u17bd\u1799\u1798\u17bc\u179b\u178a\u17d2\u178b\u17b6\u1793\u1791\u17b7\u1793\u17d2\u1793\u1793\u17d0\u1799\u17d4 \u1785\u17c6\u1796\u17c4\u17c7\u1794\u1789\u17d2\u17a0\u17b6\u1793\u17c1\u17c7\u1799\u17be\u1784\u178f\u17d2\u179a\u17bc\u179c\u1794\u1784\u17d2\u1780\u17be\u178f\u1780\u17b6\u179a\u1797\u17d2\u1787\u17b6\u1794\u17cb\u1787\u17b6\u1798\u17bd\u1799 Database \u178a\u17c4\u1799\u1794\u17be\u1780\u1780\u17b6\u179a\u1797\u17d2\u1787\u17b6\u1794\u17cb (Open Connection), Create DataSet \u178a\u17be\u1798\u17d2\u1794\u17b8\u1794\u17d2\u179a\u1798\u17bc\u179b\u1799\u1780 (fetch) \u17ac\u1794\u1789\u17d2\u1787\u17bc\u1793\u1791\u17b7\u1793\u17d2\u1793\u1793\u17d0\u1799 (submit data) \u1791\u17c5\u1780\u17d2\u1793\u17bb\u1784\u1783\u17d2\u179b\u17b6\u17c6\u1784\u1791\u17b7\u1793\u17d2\u1793\u1793\u17d0\u1799 (database) \u17a0\u17be\u1799\u1794\u17c6\u179b\u17c2\u1784\u1791\u17b7\u1793\u17d2\u1793\u1793\u17d0\u1799\u1796\u17b8 DataSet \u1791\u17c5\u1787\u17b6 .NET Objects \u17ac\u1795\u17d2\u1791\u17bb\u1799\u1798\u1780\u179c\u17b7\u1789\u178a\u17be\u1798\u17d2\u1794\u17b8 Apply \u1791\u17c5\u179b\u17be Business rules \u179a\u1794\u179f\u17cb\u1799\u17be\u1784\u17d4 \u178f\u17b6\u1798\u1796\u17b7\u178f\u1793\u17c1\u17c7\u1782\u17ba\u1787\u17b6\u1796\u17c1\u179b\u179c\u17c1\u179b\u17b6\u178a\u17c2\u179b\u178f\u17d2\u179a\u17bc\u179c\u1780\u17b6\u179a\u178a\u17c6\u178e\u17be\u179a\u1780\u17b6\u179a\u179f\u17d2\u1798\u17bb\u1782\u179f\u17d2\u1798\u17b6\u1789\u1793\u17b7\u1784\u1784\u17b6\u1799\u1793\u17b9\u1784\u1798\u17b6\u1793\u1780\u17c6\u17a0\u17bb\u179f\u17d4 \u1780\u17d2\u179a\u17bb\u1798\u17a0\u17ca\u17bb\u1793 Microsoft \u1794\u17b6\u1793\u1795\u17d2\u178f\u179b\u17cb\u1793\u17bc\u179c Framework \u1798\u17bd\u1799\u17a0\u17c5\u1790\u17b6\u201c Entity Framework\u201d \u178a\u17be\u1798\u17d2\u1794\u17b8\u1792\u17d2\u179c\u17be\u17b1\u17d2\u1799 automate \u178a\u17c2\u179b\u1791\u17b6\u1780\u17cb\u1791\u1784\u1793\u17b9\u1784 Database \u1791\u17b6\u17c6\u1784\u17a2\u179f\u17cb\u1793\u17c1\u17c7\u1791\u17c5\u179b\u17be application \u179a\u1794\u179f\u17cb\u1799\u17be\u1784 \u1794\u17d2\u179a\u179f\u17b7\u1793\u1794\u17be\u1799\u17be\u1784\u1795\u17d2\u178f\u179b\u17cb\u1793\u17bc\u179c\u1796\u17d0\u178f\u17cc\u1798\u17b6\u1793\u179b\u17c6\u17a2\u17b7\u178f\u1785\u17b6\u17c6\u1794\u17b6\u1785\u17cb\u1791\u17c5\u17a2\u17c4\u1799 Entity Framework\u17d4 \u1793\u17c5\u1780\u17d2\u1793\u17bb\u1784\u17a2\u178f\u17d2\u1790\u1794\u1791\u1793\u17c1\u17c7\u1781\u17d2\u1789\u17bb\u17c6\u1793\u17b9\u1784\u1795\u17d2\u178f\u179b\u17cb\u1780\u17b6\u179a\u178e\u17c2\u1793\u17b6\u17c6\u1781\u17d2\u179b\u17b8\u17d7\u17a2\u17c6\u1796\u17b8 Entity Framework\u17d4<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Entity Framework in ASP.NET MVC<\/strong><\/h2>\n\n\n\n<p>In this article, I am going to discuss how to use <strong>Entity Framework in ASP.NET MVC<\/strong> Application to interact with SQL Server Database to perform the CRUD operation. In the ASP.NET MVC application, the controller responds to the incoming HTTP request, then gets the data from a model and hands it over to the view. The view then renders the data. The model can be entities or business objects. Here, in this article, we are going to discuss how to retrieve the data from the SQL Server Databaseusing the Entity Framework Database First Approach in ASP.NET MVC Application. In our upcoming article, we will discuss using business objects as our model.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Creating the Database with Required Table and Data:<\/strong><\/h5>\n\n\n\n<p>Please use below SQL Script to create the&nbsp; <strong>MVC_DB <\/strong>Database, <strong>Employee<\/strong> table and populate the Employee table with test data:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">--Create a database called MVC_DB\nCREATE DATABASE MVC_DB\nGO\n\n--Use MVC_DB\nUSE MVC_DB\nGO\n\n--Create a Table called Employee\nCREATE TABLE Employee\n(\n  EmployeeId int Primary Key Identity(1,1),\n  Name nvarchar(50),\n  Gender nvarchar(10),\n  City nvarchar(50),\n  Salary decimal(18,2)\n)\nGO\n\n--Insert some values into Employee Table\nINSERT INTO Employee values('Pranaya','Male','Mumbai', 2000)\nINSERT INTO Employee values('Sambit','Male','Chennai', 3000)\nINSERT INTO Employee values('Priyanka','Female','Hydrabad', 4000)\nINSERT INTO Employee values('Anurag','Male','Cheenai', 4000)\nINSERT INTO Employee values('Subrat','Male','Mumbai', 3000)\nINSERT INTO Employee values('Preety','Female','Mumbai', 2000)\nINSERT INTO Employee values('Trupti','Female','Hyderabad', 5000)\nGO<\/pre>\n\n\n\n<p>Let us understand how to use Entity Framework in ASP.NET MVC Application step by step.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Step1: Create a new Empty ASP.NET MVC Application<\/strong><\/h5>\n\n\n\n<p>Open Visual Studio and click on the New Project link on the&nbsp;Startup&nbsp;page as shown below. Alternatively, you can also select the File menu&nbsp;-&gt;&nbsp;New Project option from the Context menu.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Creating-an-Empty-MVC-Project.png\" alt=\"Creating an Empty MVC Project\" class=\"wp-image-4584\" title=\"Creating an Empty MVC Project\"\/><\/figure>\n\n\n\n<p>From the&nbsp;New Project window as shown in the below image, expand the Visual C# node and select&nbsp;Web from the left pane. From the middle pane select ASP.NET Web Application. Provide a meaningful name to your project such as <strong>CRUD_OperationsInMVC<\/strong>. Select the location where you want to create the project by clicking on the Browse button. Finally, click on the OK button as shown in the below image.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Selecting-The-Project-Type-as-ASP.NET-Web-Application.png\" alt=\"Selecting The Project Type as ASP.NET Web Application\" class=\"wp-image-4585\" title=\"Selecting The Project Type as ASP.NET Web Application\"\/><\/figure>\n\n\n\n<p>Once you click on the OK button a new dialog will pop up for selecting project Templates as shown in the below image. From the below window, we are going to choose the Empty Project Template and Select MVC from Add Folders and core reference section.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Selecting-MVC-Template.png\" alt=\"Selecting MVC Template\" class=\"wp-image-4586\" title=\"Selecting MVC Template\"\/><\/figure>\n\n\n\n<p>You can also change the authentication by clicking on the <strong>Change Authentication<\/strong> button. You can select the appropriate authentication mode for your application. Here, we are not going to have any authentication for our application. So select the <strong>No Authentication<\/strong>&nbsp;radio button and click&nbsp;<strong>OK<\/strong>. (By default, Individual User Accounts would be selected to authenticate users stored in the SQL Server database.)<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Selecting-the-Authentication-Type-in-ASP.NET-MVC-Application.png\" alt=\"Selecting the Authentication Type in ASP.NET MVC Application\" class=\"wp-image-4587\" title=\"Selecting the Authentication Type in ASP.NET MVC Application\"\/><\/figure>\n\n\n\n<p>Wait for some time till Visual Studio creates a simple MVC project using the default template.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Step2: Adding ADO.NET Entity Data Model in ASP.NET MVC Application<\/strong><\/h5>\n\n\n\n<p>Right-Click on Models Folder, and then select <strong>Add =&gt; New Item<\/strong> from the context menu which will open the Add New Item window. From the \u201cAdd New Item\u201d window, from the left pane expand <strong>Installed =&gt; Visual C# =&gt; Data<\/strong> option. From the middle pane select the ADO.NET Entity Data Model template. Provide a meaningful name to your data model such as <strong>EmployeeDataModel <\/strong>and finally click on the<strong> Add <\/strong>button as shown in the below image.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Adding-ADO.NET-Entity-Data-Model-in-MVC-Application.png\" alt=\"Adding ADO.NET Entity Data Model in MVC Application\" class=\"wp-image-4588\" title=\"Adding ADO.NET Entity Data Model in MVC Application\"\/><\/figure>\n\n\n\n<p>From the next Entity Data Model Wizard screen, as we are going to use the <strong>Entity Framework Database First<\/strong> approach, so select<strong> EF Designer from Database <\/strong>option and click on the <strong>Next<\/strong> button as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Selecting-Entity-Framework-Database-First-Approach-in-ASP.NET-MVC-Application.png\" alt=\"Selecting Entity Framework Database First Approach in ASP.NET MVC Application\" class=\"wp-image-4589\" title=\"Selecting Entity Framework Database First Approach in ASP.NET MVC Application\"\/><\/figure>\n\n\n\n<p>In the next step, click on the new connection From Choose your data connection wizard as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Creating-new-connection-in-Entity-Framework-Database-First-Approach.png\" alt=\"Creating new connection in Entity Framework Database First Approach\" class=\"wp-image-4590\" title=\"Creating new connection in Entity Framework Database First Approach\"\/><\/figure>\n\n\n\n<p>Provide the necessary details to communicate with the database such as<strong> Server name<\/strong>, select the <strong>Authentication Type<\/strong>, select the <strong>Database<\/strong> and click on the <strong>Ok <\/strong>button as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Providing-Connection-Details-for-SQL-Server-Database.png\" alt=\"Providing Connection Details for SQL Server Database\" class=\"wp-image-4591\" title=\"Providing Connection Details for SQL Server Database\"\/><\/figure>\n\n\n\n<p>Then provide a meaningful name for the connection string that is going to save in the <strong>web.config<\/strong> file. Here, I am naming the connection string as <strong>EmployeeDBContext<\/strong> and then click on the <strong>Next<\/strong> button as shown in the below image.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Creating-Connection-String-in-Entity-Framework-Database-First-Apprach-in-MVC-Application.png\" alt=\"Creating Connection String in Entity Framework Database First Apprach in MVC Application\" class=\"wp-image-4592\" title=\"Creating Connection String in Entity Framework Database First Apprach in MVC Application\"\/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Select the version of Entity Framework<\/strong><\/h5>\n\n\n\n<p>Here I am going to use <strong>Entity Framework 6.x,<\/strong> so I am selecting the <strong>Entity Framework 6.x<\/strong> radio button and click on the <strong>Next<\/strong> button as shown in the below image.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Selecting-the-Entity-Framework-Version.png\" alt=\"Selecting the Entity Framework Version\" class=\"wp-image-4593\" title=\"Selecting the Entity Framework Version\"\/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Selecting the Database Objects<\/strong><\/h5>\n\n\n\n<p>Then we need to select the database object for our application. As our database has one table, so we need to select that <strong>Employee<\/strong> Table. Provide a meaningful namespace to your <strong>EDMX<\/strong> file and finally click on the <strong>Finish<\/strong> button as shown in the below image.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Selecting-the-Database-Object-in-Entity-Framework.png\" alt=\"Selecting the Database Object in Entity Framework\" class=\"wp-image-4594\" title=\"Selecting the Database Object in Entity Framework\"\/><\/figure>\n\n\n\n<p>Once you click on the <strong>Finish<\/strong> button, let\u2019s see what the things are created by Entity Framework. It will add the reference to the Entity Framework in the reference folder.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Entity-Framework-References-in-ASP.NET-MVC-Application.png\" alt=\"Entity Framework References in ASP.NET MVC Application\" class=\"wp-image-4595\" title=\"Entity Framework References in ASP.NET MVC Application\"\/><\/figure>\n\n\n\n<p>The framework will create the EDMX file within the Models folder.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Edmx-File-in-Models-Folder.png\" alt=\"Edmx File in Models Folder\" class=\"wp-image-4596\" title=\"Edmx File in Models Folder\"\/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>It will create the Employee Model.<\/strong><\/h5>\n\n\n\n<p>To see the Employee Model click on <strong>Employee.cs<\/strong> file, this is inside the <strong>EmployeeDataModel.tt<\/strong> File<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace CRUD_OperationsInMVC.Models\n{\n    using System;\n    using System.Collections.Generic;\n    \n    public partial class Employee\n    {\n        public int EmployeeId { get; set; }\n        public string Name { get; set; }\n        public string Gender { get; set; }\n        public string City { get; set; }\n        public Nullable&lt;decimal&gt; Salary { get; set; }\n    }\n}\n<\/pre>\n\n\n\n<p><strong>It will create the connection string in the web.config file as shown below.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"614\" height=\"138\" src=\"https:\/\/isophal.com\/wp-content\/uploads\/2023\/02\/Connection-String-in-Web-Config-File.png\" alt=\"\" class=\"wp-image-4568\" srcset=\"https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/Connection-String-in-Web-Config-File.png 614w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/Connection-String-in-Web-Config-File-600x135.png 600w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/Connection-String-in-Web-Config-File-300x67.png 300w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/Connection-String-in-Web-Config-File-585x131.png 585w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/figure>\n\n\n\n<p>It will create the <strong>DBContext<\/strong> class for us whose name is the same as the name of the connection string that is <strong>EmployeeDBContext. <\/strong>The <strong>EmployeeDBContext&nbsp;<\/strong>class derives from the <strong>DbContext&nbsp;<\/strong>class and is responsible for establishing a connection to the database. The&nbsp;<strong> EmployeeDataModel.Context.cs<\/strong> which is inside <strong>EmployeedataModel.Context.tt<\/strong> as sown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"456\" height=\"130\" src=\"https:\/\/isophal.com\/wp-content\/uploads\/2023\/02\/DbContext-Class.png\" alt=\"\" class=\"wp-image-4569\" srcset=\"https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/DbContext-Class.png 456w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/DbContext-Class-300x86.png 300w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/figure>\n\n\n\n<p><strong>Below is the auto-generated code for the EmployeeDBContext class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace CRUD_OperationsInMVC.Models\n{\n    using System;\n    using System.Data.Entity;\n    using System.Data.Entity.Infrastructure;\n    \n    public partial class EmployeeDBContext : DbContext\n    {\n        public EmployeeDBContext()\n            : base(\"name=EmployeeDBContext\")\n        {\n        }\n    \n        protected override void OnModelCreating(DbModelBuilder modelBuilder)\n        {\n            throw new UnintentionalCodeFirstException();\n        }\n    \n        public virtual DbSet&lt;Employee&gt; Employees { get; set; }\n    }\n}<\/pre>\n\n\n\n<p>That\u2019s it. We successfully created our Entity Data Model. Now it\u2019s time to use this model in our application.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Creating Controller:<\/strong><\/h5>\n\n\n\n<p>Right-Click on Controllers Folder. Select <strong>Add =&gt; Controller =&gt; Select MVC5 Controller Empty <\/strong>and then click on add button as shown in the below image.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Creating-MVC-5-Controller.png\" alt=\"Creating MVC 5 Controller\" class=\"wp-image-4599\" title=\"Creating MVC 5 Controller\"\/><\/figure>\n\n\n\n<p>In the next Screen provide the controller name as <strong>EmployeeController<\/strong> and click on the <strong>Add<\/strong> button as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/dotnettutorials.net\/wp-content\/uploads\/2018\/07\/Providing-Controller-Name.png\" alt=\"Providing Controller Name\" class=\"wp-image-4600\" title=\"Providing Controller Name\"\/><\/figure>\n\n\n\n<p>Let\u2019s modify the default route in the <strong>RouteConfig<\/strong> class so that when the application runs for the first time it will redirect to the Index Action method of Employee Controller.<\/p>\n\n\n\n<p>After modifying the <strong>RouteConfig<\/strong> class the codes looks like as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"490\" src=\"https:\/\/isophal.com\/wp-content\/uploads\/2023\/02\/image-2-1024x490.png\" alt=\"\" class=\"wp-image-4570\" srcset=\"https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-2-1024x490.png 1024w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-2-600x287.png 600w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-2-300x144.png 300w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-2-768x367.png 768w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-2-1536x735.png 1536w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-2-1170x560.png 1170w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-2-585x280.png 585w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-2.png 1871w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace CRUD_OperationsInMVC\n{\n    public class RouteConfig\n    {\n        public static void RegisterRoutes(RouteCollection routes)\n        {\n            routes.IgnoreRoute(\"{resource}.axd\/{*pathInfo}\");\n\n            routes.MapRoute(\n                name: \"Default\",\n                url: \"{controller}\/{action}\/{id}\",\n                defaults: new { controller = \"Employee\", action = \"Index\", id = UrlParameter.Optional }\n            );\n        }\n    }\n}<\/pre>\n\n\n\n<p><strong>Let\u2019s modify the index action method of employee controller to use entity framework as shown below<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace CRUD_OperationsInMVC.Controllers\n{\n    public class EmployeeController : Controller\n    {\n        public ActionResult Index()\n        {\n            EmployeeDBContext dbContext = new EmployeeDBContext();\n            List&lt;Employee&gt; empList = dbContext.Employees.ToList();\n            return View(empList);\n        }\n    }\n}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Let\u2019s create the index view.<\/strong><\/h5>\n\n\n\n<p>Right-click on the Index Action Method and click on Add View. Then paste the following code in the Index,cshtml file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"714\" src=\"https:\/\/isophal.com\/wp-content\/uploads\/2023\/02\/image-3-1024x714.png\" alt=\"\" class=\"wp-image-4571\" srcset=\"https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-3-1024x714.png 1024w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-3-600x418.png 600w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-3-300x209.png 300w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-3-768x536.png 768w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-3-1170x816.png 1170w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-3-585x408.png 585w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-3.png 1246w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">@model IEnumerable&lt;CRUD_OperationsInMVC.Models.Employee&gt;\n&lt;table class=\"table\"&gt;\n    &lt;tr&gt;\n        &lt;th&gt;\n            @Html.DisplayNameFor(model =&gt; model.Name)\n        &lt;\/th&gt;\n        &lt;th&gt;\n            @Html.DisplayNameFor(model =&gt; model.Gender)\n        &lt;\/th&gt;\n        &lt;th&gt;\n            @Html.DisplayNameFor(model =&gt; model.City)\n        &lt;\/th&gt;\n        &lt;th&gt;\n            @Html.DisplayNameFor(model =&gt; model.Salary)\n        &lt;\/th&gt;\n    &lt;\/tr&gt;\n\n@foreach (var item in Model) {\n    &lt;tr&gt;\n        &lt;td&gt;\n            @Html.DisplayFor(modelItem =&gt; item.Name)\n        &lt;\/td&gt;\n        &lt;td&gt;\n            @Html.DisplayFor(modelItem =&gt; item.Gender)\n        &lt;\/td&gt;\n        &lt;td&gt;\n            @Html.DisplayFor(modelItem =&gt; item.City)\n        &lt;\/td&gt;\n        &lt;td&gt;\n            @Html.DisplayFor(modelItem =&gt; item.Salary)\n        &lt;\/td&gt;\n    &lt;\/tr&gt;\n}\n&lt;\/table&gt;<\/pre>\n\n\n\n<p>That\u2019s it. Now run the application and you should get the result as expected as shown in the below image.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"596\" height=\"631\" src=\"https:\/\/isophal.com\/wp-content\/uploads\/2023\/02\/image-4.png\" alt=\"\" class=\"wp-image-4572\" srcset=\"https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-4.png 596w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-4-283x300.png 283w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2023\/02\/image-4-585x619.png 585w\" sizes=\"auto, (max-width: 596px) 100vw, 596px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u17d4 \u1780\u17d2\u179a\u17bb\u1798\u17a0\u17ca\u17bb\u1793 Microsoft \u1794\u17b6\u1793\u1795\u17d2\u178f\u179b\u17cb\u1793\u17bc\u179c Framework \u1798\u17bd\u1799\u17a0\u17c5\u1790\u17b6\u201c Entity Framework\u201d \u178a\u17be\u1798\u17d2\u1794\u17b8\u1792\u17d2\u179c\u17be\u17b1\u17d2\u1799 automate \u178a\u17c2\u179b\u1791\u17b6\u1780\u17cb\u1791\u1784\u1793\u17b9\u1784 Database \u1791\u17b6\u17c6\u1784\u17a2\u179f\u17cb\u1793\u17c1\u17c7\u1791\u17c5\u179b\u17be application \u179a\u1794\u179f\u17cb\u1799\u17be\u1784 <\/p>\n","protected":false},"author":1,"featured_media":4573,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189,140],"tags":[575,570,576,577],"class_list":["post-4567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net","category-programming","tag-ast-net","tag-crud","tag-entity-framework","tag-mvc"],"_links":{"self":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/comments?post=4567"}],"version-history":[{"count":2,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4567\/revisions"}],"predecessor-version":[{"id":4805,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4567\/revisions\/4805"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media\/4573"}],"wp:attachment":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media?parent=4567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/categories?post=4567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/tags?post=4567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}