Entity Framework questions Flashcards
What is Entity Framework ?
- Bhanu Prakash Kola
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.
What is DbSet?
- Yasaswini.G
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.
What is DBcontext ?
- Rizwan Ahamed
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.
What is ORM ?
- Rizwan Ahamed
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.
What are the different types of approaches in EF?
- Bhanu Prakash Kola
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.
what is Lazy Loading, Eager Loading, Explicit Loading ?
- Manoj K
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.
What are different entity states in EF
- Bhanu Prakash Kola
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.
Explain POCO Classes in EF ?
- Srinu S
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.
What is Migration and its Types?
~ Mahathi
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:
- 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.
- 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.
Explain the ObjectSet in EF.
~Venkata Teja
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.
Explain how EF supports transactions.
~Venkata Teja
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.
What is the best way to handle SQL injection attacks in Entity Framework?
- Srinu S
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 to improve performance of Entity Framework?
-Prasanna B
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.
What are the steps to retrieve data from db using Entity Framework in MVC?
-Prasanna B
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>
What is ObjectContext ?
-Yasaswini.G
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.