Getting Started With Entity Framework

Introduction

This article will cover the following things,

  • Brief Concepts
  • Implementation – Setup Console Application, using EF.
  • Implementation – Create DB, using EF Code First approach.
  • Implementation – Configure SQL Server. Check the DB and tables.
  • Implementation – Implement CRUD operations using EF, using Code First approach.

What is Entity Framework?

  • Entity framework is an open source ORM framework from Microsoft.
  • It is an enhancement to ADO.NET, which gives the developers an automated mechanism for accessing & storing the data in the database.
  • Entity framework is useful in two scenarios.

    • First, if you already have an existing database ahead of other parts of the Application.
    • Second, you want to focus on your classes and then create the database from your classes.

      Entity Framework
What is O/RM?
  • ORM is a tool for storing the data from the domain objects to the relational database like MS SQL Server in an automated way.
  • O/RM includes parts:

    • Domain class objects
    • Relational database objects

  • Mapping information on how the domain objects map to the relational database objects (tables, views and the stored procedures).
  • It also automates the standard CRUD operation (Create, Read, Update & Delete) so that the developer doesn't need to write it manually.

Implementation – Setup Console application using EF

Steps to be followed, 

  • Open VS 2015 and create a project.
  • Choose Visual C# and console Application.

    console application
  • Right click on the solution. Click on “Manage NuGet Packages” and browse for Entity framework.

    Manage Nuget Packages
  • Install the package with the latest stable version.

    package
  • Create a model class “Employee” with the possible properties.

    Employee
  • Add the connection strings settings for e.g. DB Name and Data Source into the App.config file.

    code
  • Add the DB context file for the employees names as “EmployeeDbContext”, using the name of the connection string into the base constructor.

    code
  • Now, we have created the console Application, using EF and we are good to go now for the creation of the DB.

Implementation – Create DB using EF Code First approach

Steps to be followed,

  • Go to the program.cs file in the console Application and write a method for the creation of the DB.

    code

Implementation – Configure SQL Server, check the DB and tables

Steps to be followed,

  • Open SQL Server and connect to the DB, using Windows credentials.

    credentials
  • In SQL Server, check for the DB (Demo) and tables (Employees) created, using EF.

    EF

Implementation – Implement CRUD operations using EF using Code First approach

READ FUNCTIONALITY

Steps to be followed,

  • Open program.cs and create a method Read () to implement GET functionality.

    code
  • Run the Application and see the output.

    output
  • Look for the row in “Demo” DB and within the table “Employees”.

    code

INSERT FUNCTIONALITY

Steps to be followed,

  • Open program.cs and create a method Insert () to implement an add functionality.

    code
  • Look for the inserted row in “Demo” DB and within the table “Employees”.

    code

UPDATE FUNCTIONALITY

Steps to be followed,

  • Open program.cs and create a method Update () to implement the update functionality.
    code
  • Look for the updated row of EmpId-3 in “Demo” DB and within the table “Employees”.

    code

DELETE FUNCTIONALITY

Steps to be followed,

  • Open program.cs and create a method Delete () to implement an add functionality.

    code
  • Delete the row with an EmpId -3 in “Demo” DB and within the table “Employees”.

    code


Similar Articles