Entity Framework questions Flashcards

1
Q

What is Entity Framework ?

                                                      - Bhanu Prakash Kola
A

Entity Framework is an Object Relational Mapper (ORM) developed my microsoft to work with .Net Applications.
EF allows developers to create and manage database schemas and also perform CRUD(Create,Read,Update,Delete) operations on the database using .Net objects.
There are 2 types of approaches to Entity Frameworks,
1.Database First Approach:It will create the entities and
DBcontext for us based on the database.
2.Code First Approach:It will create the database for you and
establish the relationships as well based on the models.
The Benefits of using EF Core are
1.The developer team need not to be expert in complex SQL operations,
2.Querying using EF is simply done by using Linq.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is DbSet?

                                                    - Yasaswini.G
A

DbSet is a typed entity set that is used to perform create, read, update, and delete operations on a particular entity. DbSet can only be created from a DbContext instance. DbSet does not support Entity SQL methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is DBcontext ?

                                                        -  Rizwan Ahamed
A

It is an important class in Entity Framework API. This is used to connect a domain class or entity and the database. Its main responsibility is to communicate with the database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is ORM ?

                                                      - Rizwan Ahamed
A

Object Relational Mapper - It helps to map Server side language objects to relational entities of database

By Using ORM the developer is at ease as not much of proficiency is required to have appliaction connected with database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different types of approaches in EF?

                                                         - Bhanu Prakash Kola
A

Code First Approach: The Code First approach is mainly used to create a model and its relationships using classes, which further help create a database. This enables developers to work in an object-oriented manner without thinking about the structure of the database.

Database First Approach: The Database First approach used in the context of object-relational mapping (ORM), which involves designing and creating the database schema first, and then generating the corresponding code from it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is Lazy Loading, Eager Loading, Explicit Loading ?

                                                              - Manoj K
A

Lazy Loading: Is a technique in which data is only loaded from the database when it is actually needed.

Eager Loading: It occurs when you query for an object and all of the related objects are also returned. In eager loading, related objects are loaded automatically with its parent object.

Explicit Loading: Explicitly loading takes place when you have disabled Lazy loading, and you still want to lazy loading.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are different entity states in EF

                                                         - Bhanu Prakash Kola
A

Unchanged: The entity has not been modified since it was retrieved from the database, and its state is set to Unchanged.

Added: The entity is new and has been added to the database, but has not yet been saved. Its state is set to Added.

Modified: The entity has been modified, and its changes have not yet been saved. Its state is set to Modified.

Deleted: The entity has been marked for deletion, and will be removed from the database the next time changes are saved. Its state is set to Deleted.

Detached: The entity is not being tracked by the context. This can occur if the entity was created outside of the context or if it was loaded from the database but then detached from the context.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain POCO Classes in EF ?
- Srinu S

A

POCO (Plain Old CLR Objects) classes in Entity Framework are simple C# or VB.NET classes that represent entities in your database. POCO classes are simple classes that are not dependent on any framework or technology, and they do not inherit from any base classes or implement any special interfaces. POCO classes can be used with any ORM (Object-Relational Mapping) tool, and they can be used in non-ORM scenarios as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Migration and its Types?
~ Mahathi

A

Migration is a tool that was introduced in EF to update the database schema automatically when a model is modified without losing any data or other objects. Migrate Database To Latest Version is a new database initializer used by it. Entity Framework offers two types of migration:

  1. Automated Migration: Entity Framework 4.3 was the first to introduce automated migration so you don’t have to manually migrate databases every time you alter a domain class. For example, you must also change the domain classes for each time you make a change, but with automated migration, you can simply run a command through the Package Manager Console.
  2. Code-based Migration: When you use a code-based migration, you can configure additional aspects of the migration, like setting the default value of a column, configuring a computed column, etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain the ObjectSet in EF.
~Venkata Teja

A

ObjectSet is generally considered as a specific type of data set that is commonly used to read, update, create, and remove operations from existing entities. Only the ObjectContext instance can be used to create it. No Entity SQL method is supported by it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain how EF supports transactions.
~Venkata Teja

A

The SaveChanges() method in EF always wraps any operation involving inserting, updating, or deleting data into a transaction. Hence, you do not have to explicitly open the transaction scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the best way to handle SQL injection attacks in Entity Framework?

      - Srinu S
A

SQL injection is a method that hackers use to access sensitive information from an organization’s database. The most common cause of SQL Injection is that user input fields allow SQL statements to pass through and directly query the database

The injection-proof nature of Entity Framework lies in the fact that it generates parameterized SQL commands that help prevent our database from SQL injections.

It is best to never combine user inputs with Entity SQL commands text to prevent or avoid this problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to improve performance of Entity Framework?
-Prasanna B

A

Do not put all DB objects into one entity model.
Disable the entity which is no longer required.
Optimize and debug LINQ queries
Use indexes properly.
Use pre-generating Views to reduce response time for the first request.
Limit the resultset size.
Avoid using Views and Contains.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the steps to retrieve data from db using Entity Framework in MVC?
-Prasanna B

A

Create a new instance of the DbContext class to connect to the database.
Create a new instance of the DbSet<T> class for the entity you want to retrieve data for.
Use LINQ to query the DbSet<T> for the data you need.
Execute the query by calling a method like ToList() or Single() to retrieve the data from the database.
Pass the data to your view using a model.</T></T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is ObjectContext ?

                                            -Yasaswini.G
A

ObjectContext is a class that manages all the database operations, like database connection, and manages various entities of the Entity Model. We can say that ObjectContext is the primary class for accessing or working together with entities that are defined in the conceptual model.
it Provides facilities for querying and working with entity data as objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What can we do to improve the performance of the Entity Framework?
~ Mahathi Somisetty

A

We can use the following ways to improve the performance of the Entity Framework:

  1. We can use compiled queries whenever required.
  2. We must avoid the use of Views and Contains.
  3. We can disable and alter tracking for the entity when it is not needed.
  4. We can Debug and Optimize the LINQ query.
  5. We should retrieve only the desired number of records when binding data to the g.