CRUD Operation In Entity Framework With MVC - Part 1

Introduction

We develop applications with help of entity framework to create database first and use scaffolding MVC features .

Database First Approach

Entity Framework built-in features to automatically generate a data model with classes and properties of existing database tables and columns. Entity Framework provide graphical interface for displaying and editing .edmx file.

Set Up SQL Server Database

Step 1: Create database.

Figure 1.1: How to Create New Database.

Right click on Databases folder and then click New Database:

NewDatabase
                                           Figure 1.1

Step 2: Database name

A new window will open, enter Database Name like CRM, DR, RR. After that enter Database name and click OK button. Here's figure 1.2:

Enter Database Name
                                                           Figure 1.2

Step 3: Create table and see the database.
  1. Create Table to use below script  
  2. Create Table EmpMst (EmpId Int Identity(1,1) Primary Key,  
  3. EmpName Varchar(100),  
  4. EmpAddress Varchar(200),  
  5. PhoneNo Varchar(18),  
  6. Email varchar(50))  
After creating a table you can now see table structure as in the following figure 1.3.

Create Table
                                                           Figure 1.3

Adding Database in MVC Application


For adding database entities right click Models Folder. See context menu and Add New Items. Every table create individual classes. You can add .edmx project in any folder.


                                                                  Figure 1.4
Add New Item window will open. You can select ADO.Net Entity Data Model and enter name. Please enter name CRMX,edmx and click Add button

ADO.NET
                                                                           Figure 1.5

After clicking add button open Entity Data Model Wizard window. You can select Generate from database and click Next button as in the following figure 1.6:

Generate from database
                                                  Figure 1.6

After clicking Next open a new winodw to select connection. Click New Connection as in the following figure 1.7 or already available connection and then select dropdown list.

New connection
                                         Figure 1.7

After clicking New Connection open windows Connection Properties and you can add Data Source and Server Name. Select Authentication Type Windows or SQL Server. Select database name already created in SQL Server. Here's  figure 1.8:

Connection
                                     Figure 1.8

Check connection as in the following figure 1.9. If connection is successful then alert message is successful, else failed. Click OK button.

test connection
                                        Figure 1.9

After clicking OK button connection name and connection string display in window. Also, you can enter entities name. Click Next  as in the following figure 1.10.

Click Finish
                                            Figure 1.10

You can add the following object: Tables, Views, Stored Procedures and Functions. You need to select object and click Finish  as in the following figure 1.11.

Table
                                          Figure 1.11

You can see design view in database available in tables.
 
Figure 1.12 showing Design View is .edmx file and you can see Solution Explore .edmx file.

Navigation
                                                                               Figure 1.12

 


Similar Articles