Thursday, 29 September 2016

Entity Framework

What is Entity Framework?

Entity Framework in asp.net is the enhancement of ADO.net which provides the strong mapping facilities. It is an ORM (Object Relational Mapping) which helps to interact with Relational Database using object. It provides the higher level of abstraction.

How many Type of approaches available in Entity Framework?

There are 3 approaches in Entity Framework
  • Database First
  • Model First
  • Code First
Database First
Database first is the approach in which our database has already generated. And using this database we create our Model and code in entity framework.
Model First
In Model first we create designer (model) first. If database not exist then we can create database using this designer
Code First
In code first we create code file first and then if database not exist then database will be created automatically at runtime using this code.

What is difference between LINQ to SQL and Entity Framework?

The major difference between LINQ to SQL and Entity Framework is that LINQ to SQL support only SQL Server but Entity Framework supports almost all Relational Database.
LINQ to SQL provides limited mapping facility but entity framework provides full mapping facility.

Entity Framework Interview Questions List

  1. What is Entity Framework?
  2. What are the advantages of using Entity Framework?
  3. What is the Nuget Package console command to install Entity Framework, if it’s not there in you project at that time?
  4. Where do we use Virtual classes in Entity Framework DbContext Models?
  5. Entity Framework uses DbContext, How is it different from Object Context?
  6. What’s new in Entity Framework 6?
  7. Can we access a model in Entity Framework without primary key defined?
  8. Why do we use Lazy Loading in Entity Framework?
  9. Why DB Context models object disposal is important in Entity Framework?
  10. Can we do an insertion and deletion at the same block of code and then Apply save changes to them in a statement in Entity Framework?
  11. What are the development approaches are supported in Entity Framework?
  12. What is the query syntax we use to query an ADO.NET Entity Data Model?
  13. Is LINQ a feature of Entity Framework?
  14. Does Default New Project Creation under Internet Template Category of ASP.NET MVC have any DB Context Ready while creating the project?
  15. If you provide the Primary Key value while inserting record to Entity Framework, will that execute fine, or will it throw an error while the adding statement?
  16. What are the properties or Database Elements get copied in Dataset when Clone method is used?
  17. What is the role of a Self-Tracking Entities?
  18. Can you describe the feature of split entity in Entity Framework?
  19. What is the role of Entity Container in Entity Framework?
  20. Do we need to define any assembly refinances of entity framework if we use in web.config file?

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). It enables us to interact with relational data in an object oriented way, meaning we can retrieve and manipulate data as strongly typed objects using LINQ queries.Microsoft Entity Framework
Microsoft introduced Entity Framework with .NET Framework v3.5 Service Pack 1 supporting basic Object Relational Mapping support but later EF4 was introduced as second version aligning with .NET Framework v4.0.
Back to top

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
Back to top

Where do we use Virtual classes in Entity Framework DbContext Models?

We use Virtual classes in Entity Framework in context class where we define DBSet of corresponding table. As we can see easily in below code sample for Students and Departments:
Entity Framework Interview Questions Virtual Classes
Back to top

Entity Framework uses DbContext, How is it different from Object Context?

DbContext can be used for code first while Object Context doesn’t. It exposes the most commonly used features of ObjectContext.
Back to top

What’s new in Entity Framework 6?

  • Customizing Code First Conventions.
  • Logging of database commands.
  • Stored Procedure Mapping.
  • Asynchronous Queries and Save support.
Whats New In EF6
With EF6, in applications with lots of tables and relationships defined, our context objects open faster. We should also be better insulated from dropped connections (at least, if the drop is transient and not permanent — nothing is going to help there).  EF6 also generates SQL faster from LINQ queries than before (though it’s the same SQL that’s being generated as in earlier versions of EF, so your actual data access won’t be any faster or slower).
Back to top

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

No, but we can access data.
Using Primary Key:
Student student = db.Students.Find(id);
Without Primary Key:
Student student = db.Students.Where(x=>x.StudentName==”Ahmad”);

Why do we use Lazy Loading in Entity Framework?

Entity Framework facilitates us to avoid huge, deep, highly-relational queries, so that the data will not heavy on the web.

Why DB Context models object disposal is important in Entity Framework?

Until it’s disposed, it will be holding resources that aren’t in. If not disposed then Garbage collector will free the space but in some instance it holds up.
Calling dispose method to clear memory.Dispose in Entity Framework
Back to top

Can we do an insertion and deletion at the same block of code and then Apply save changes to them in a statement in Entity Framework?

Yes, WE CAN by using the below piece of code:
Save changes in Entity Framework
Here the student record will insert to db but not physically. When we call SaveChanges method then it actually insert in db and commit the transaction. If we delete before committing transaction nothing will change in database.
Back to top

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:

Is LINQ a feature of Entity Framework?

Yes, following is the example to get student record from Section A.
LINQ feature in EF6
The same in Entity Framework:
Student student = db.Students.Where(x=>x.Section==”A”);
Back to top

Does Default New Project Creation under Internet Template Category of ASP.NET MVC have any DB Context Ready while creating the project?

Yes, There is, as we can see in the below screenshot:Template Category For MVC
Back to top

If you provide the Primary Key value while inserting record to Entity Framework, will that execute fine, or will it throw an error while the adding statement?

Yes it will threw error if same a record present with same data. If identity is set then it will give error in any case.Insert In EF6
Back to top

What are the properties or Database Elements get copied in Dataset when Clone method is used?

It will create a new object with same properties with a new instance.Clone Method in EF6
Back to top

What is the role of a Self-Tracking Entities?

Self-tracking entity allows you to add code generated item:Self Tracking Entities in EF6
Back to top

Can you describe the feature of split entity in Entity Framework?

Entity splitting gives us the ability to take an entity in our model and split this entity into multiple database tables. When we query an entity, Entity Framework will create a query that Automatically joins the related physical tables for us.
Back to top

What is the role of Entity Container in Entity Framework?

Entity container is a logical grouping of entity sets, association sets, and function imports.  The following must be true of an entity container defined in a conceptual model:
At least one entity container must be defined in each conceptual model.  The entity container must have a unique name within each conceptual model.

Do we need to define any assembly references of entity framework if we use in web.config file?

Yes, that is necessary:web.config for Entity Framework

No comments:

Post a Comment