Wednesday, 30 November 2016

Entity Framework

What is Entity Framework?

Microsoft Entity Framework (EF) is an Object Relational Mapping framework that provides an abstraction layer (a kind of mapping) between two incompatible systems (i.e. Object oriented programming languages with Relational databases).
Microsoft Entity Framework

What are the advantages of using Entity Framework?

Main advantages of Entity Framework are:
  • Separation of Concerns (SoC).
  • LINQ is used as a common syntax for all object queries.
  • Code Auto-Generation.

What is the Nuget Package console command to install Entity Framework, if it’s not there in you project at that time?

Following is the Nuget Package console command to install Entity Framework:
Install-Package EntityFramework -Version 6.1.1Install Entity Framework -Version 6.1.1

How to Install Entity Framework via NuGet Package in Visual Studio?

It’s quite simple to install Entity Framework via Nuget for our project in Visual Studio. For example, If I have just created a new project and want to install Entity Framework, below step by step approach will be helpful.
  1. Right click on the project and choose “Manage NuGet Packages…”.
  2. On Installation page, choose “Browse” Tab from top left and then enter “Entity Framework” in search box below that and click on search icon. It will list down all Entity Framework related results below. As you can see, we have selected Entity Framework and all details (including version, author, License, Date Published etc.) of the selected item are displayed in right area.
  3. In above screenshot, click on Install button. It will ask for a Review Changes going to be done, confirm by clicking “YES”.
  4. It followed by a License Acceptance confirmation. Just confirm by clicking “I Accept”.
  5. Once you accept the License, it will complete the installation displaying details in Visual Studio Output Window as shown in below screenshot. Finally, you can see the successful installation message in Output Window. Once the installation is done successfully, you can see EntityFramework and EntityFramework.SqlServer in References of our project.

Can we access a model in Entity Framework without primary key defined?

No, but we can access data.

What are the development approaches are supported in Entity Framework?

  • Code First Approach – where code defines the database. Entity Framework handles creation.
  • Database First Approach – regular approach used where database is first created or already exists.
  • Model First Approach – where model is drawn first that further generate database scripts.

What is the query syntax we use to query an ADO.NET Entity Data Model?

We can use LINQ to Query ADO.Net Entity Framework. For Example:
LINQ feature in EF6

LINQ to SQL Vs Entity Framework?

Following table clearly explains the difference between LINQ to SQL Vs Entity Framework:
Entity FrameworkLINQ to SQL
Work with any database including SQL Server, Oracle, MySQL, DB2 etc.Works but limited to SQL Server.
Entity Framework works well in enterprise scenarios where requirement is to develop a loosely coupled application.LINQ to SQL is good in rapid application development scenarios but follows a tightly coupled approach.
Entity Framework maintain a conceptual model that actually maps to storage model using a mapping. So, it’s possible that multiple EF classes map to one table or multiple tables map to one EF class.A LINQ Entity directly maps to one SQL Server table only. In order to maintain a relationship, it generates a .dbml file.
Can generate database from model and it’s a key feature.Generate database from model is not possible.

No comments:

Post a Comment