Entity Framework Overview

Entity Framework Overview

Entity Framework Overview

Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.

Entity Framework is a part of the Net.Framework. EF allows us to access relational databases using strongly typed C Sharp core, or Visual Basic code or really any .Net language. There are three ways to access databases. Shema first, Model first and Code first. While working with classes to talk the Entity Framework, you don't have to worry about SQL connections, SQL commands, SQL parameters… There’s a few different ways to get started with the Entity Framework. One approach is called the Shema First approach. In this approach, you open up a graphical designer in Visual Studio, you point it to an existing database, and it can import the database schema and generate all the classes you need to query and update that database. Another approach is a Model First approach. We use the same graphical designer in Visual studio to draw a conceptual model for applications, so what classes do I want. Then EF generate both my class definitions and database schema. Finally, there’s a Code First approach you can take with EF. In this approach, it is necessary write C Sharp classes and the EF can use those class definitions to create a database. It will do that either using conventions like naming conventions. Naming conventions may make some things work if we write them in the right way. "Entity Framework" knows how to work with various types of databases (SQL Server, SQL Compact, Oracle, DB2…). We can create an application without much worries about the database type.

Relational data base

Picture 1. Access a relational data base


Code First - creating a new database

The first step after creating a new project is to create a Model. So right click on Models/Add /Class and add class (example 1).

using System;
using System.Collections.Generic;
namespace WebSkProject.Models
    public class Student
        public int StudentID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public DateTime EnrollmentDate { get; set; }

Example 1. Student class

The second step is to create "Context". It allows us to communicate with the database and store data in tables. It is important to add here to use "System.Data.Entity".

using System.Data.Entity;
using WebSkProject.Models;
namespace WebSkProject.Contexts
    public class RestourantDb : DbContext
        public RestourantDb() : base("name=CodeFirst")
        public DbSet<Restoraunt> Restoraunts { get; set; }
        public DbSet<RestaurantRewiew> Reviews { get; set; }

Example 2. Context

Created "RestaurantDb" was inherited from "DbContext". For each class in the model, it is necessary to write the property „DbSet< TEntity >“. When it was created Model and Contex you have to write "ConnectionString" in the file „WebConfig“.This is all the code that should be written for storing data in the database.

    <add name="CodeFirst" connectionString="Data Source=DESKTOP-LJ085FJ\SQLEXPRESS; database = Restaurant; Integrated Security=True" providerName="System.Data.SqlClient" /></connectionStrings>

Example 3. WebConfig connecionString


The connection string is part of a global code that serves to connect to the server and to the database. In "connectionString", "name" is the name of the connection, "Data Source" is the server to which we are joining (in this case SQL localhost), and "database" is the name of the database we are creating.

Model First - creating a new database

This approach allows creating a model based on which database will be generated. The tool to create this model in Visual Studio is on Project/AddNew Item/Data/ADO.NET Entity Data Model/Empty EF Designer model. Once the installation is completed, Visual Studio opens a graphical interface for creating a database. Right clicking on the interface opens the drop-down menu where we select Add New / Entity.

Mvc 5. Model View Controller

Picture 2. Creating the "Hotels" table


In the new menu, we create the table name and "primary key". By right-clicking on table and „Add New“ we add the other properties of this table. When all tables and their links are matched, it is necessary to generate a database from the schema that we have made.

We do this by clicking the right click on the interface and choosing „Generate Database from Model“. In the next menu, we can select an existing connection to the server or create a new one. Also, we can select an existing database or create a new one. The next step generates a database based on the SQL script that was created in the project. After starting this script, a database will be created.

Mvc 5. Model View Controller

Picture 3. Graphic view of the model


This tool allows you to start with a clean design interface and make a model, or to start with a database that already exists and generate table copies in this interface. It also allows for updating the database when changing the model. The Entity Framework is designed for business application development and supports many types of databases. The EF advantage is that it enables easy and quick operations such as creating, reading, updating, and deleting data. The biggest advantage is productivity. From the disadvantages it would be stated that if the database schema changes, the model will not be automatically updated.

Posted by Josip Golubovic

Josip Golubovic

Related Posts


comments powered by Disqus