Entity Framework, Serinity Framework
Entity Framework, Serinity Framework
• If the developer does not use raw SQL codes, things can become complicated
sometimes.
• It is a slower form of the Object Relational Mapper.
• For a big domain model, it's not ideal.
• Some RDMS do not offer this feature.
• EF's main drawback is its lazy loading
• This requires a non-traditional approach to handling data that isn't available for
every database.
• Since the data migration functionality is weak, it isn't fully effective in practice.
The Entity Data Model consists of 3 core components that form the basis for Entity
Framework. The three main components of EDM are as follows:
First of all, a database lets you reverse engineer a model from an existing database.
Entity Framework Designer is used to view and edit models stored and created in EDMX
files (.edmx extensions). Using the EDMX file, you automatically generate classes that
you can interact with within your application.
EDMX files represent conceptual models, storage models, and their mappings. This file
contains all the mapping information between SQL tables and objects. In addition, it
also includes essential information required for rendering models graphically with
ADO.NET Entity Data Designer. Furthermore, it is divided into three divisions, CSDL, MSL,
and SSDL.
• Code First Approach: The Code First approach primarily uses classes to create
the model and its relations, which are then used to create a database. This way,
developers can work in an object-oriented manner without considering the
database structure. By following this model, developers first write POCO classes
and then use these classes to create the database. Code First is the method used
by most developers using Domain-Driven Design (DDD).
• Model First Approach: In contrast, the Model First approach uses ORM to build
model classes and their relationships. Following the successful creation of the
model classes and relationships, the physical database is created using these
models.
• Database-First Approach: In Entity Framework, Database First approach is used
to build entity models based on existing databases and reduce the amount of
code required. By using this approach, domain and context classes can be
created based on existing classes.
It is impossible to define one approach as the optimal approach when using the Entity
Framework. Project requirements and the type of project determine which development
approach should be used. Database First is a good approach if there is a database
present. Model First is the optimal choice if no database and model classes exist. As
long as the domain classes are available, the Code First method is the best choice.
10. What do you mean by the term navigation property in the entity
framework?
• Added: It is a state in which an entity exists within the context but does not exist
within the database. When the user invokes the SaveChanges method, DbContext
usually generates an INSERT SQL query to insert the data into the database. Upon
successful completion of the SaveChanges method, the entity's state changes to
unchanged.
• Deleted: This state indicates that the entity is marked for deletion has not been
removed from the database. Also, it indicates the existence of the entity in the
database. When the user invokes the SaveChanges method, DbContext usually
generates a DELETE SQL query to delete or remove the entity from the database.
Upon successful completion of the delete operation, DbContext removes the
entity.
• Modified: When the entity is modified, its state becomes Modified. Also, it
indicates the existence of the entity in the database. When the user invokes the
SaveChanges method, DbContext usually generates an UPDATE SQL query to
update the entity from the database. Upon successful completion of the
SaveChanges method, the entity's state changes to unchanged.
• Unchanged: Since the context retrieved the entity's property values from the
database, the values have not changed. This entity is ignored by SaveChanges.
• Detached: This state indicates that the entity is not tracked by the DbContext. If
an entity was created or retrieved outside the domain of the current instance of
DbContext, then its entity state will be Detached.
The following diagram represents the different entity states in Entity Framework:
12. Write the importance of the T4 entity in Entity Framework.
In Entity Framework code generation, T4 files are crucial. EDMX XML files are read by T4
code templates, which generate C# behind code. The generated C# behind code
consists only of your entity and context classes.
• CSDL: This stands for Conceptual Schema Definition Language. Basically, it's a
conceptual abstraction that is exposed to the application. In this file, you will find
a description of the model object.
• SSDL: This stands for Storage Schema Definition Language. In this section, we
define the mapping to our RDBMS data structure.
• MSL: This stands for Mapping Schema Language. SSDL and CSDL are connected
by it. It bridges the gap between the CSDL and SSDL or maps the model and the
storage.
14. Explain the ways to increase the performance of EF.
15. Write some XML generation methods provided by the dataset object.
20. Write the steps to retrieve data from database using Entity
Framework in MVC.
The following steps will show you how to retrieve data from a database in MVC (Model
View Controller) using Entity Framework:
DbSet: An entity set is represented by a DbSet class that can be used for creating,
reading, updating, and deleting operations on it. Those DbSet type properties, which
map to database tables and views, must be included in the context class (derived from
DbContext).
DbContext: It is considered an essential class in EF API that bridges the gap between an
entity or domain class and the database. Communication with the database is its
primary responsibility.
23. Explain the role of Pluralize and Singularize in the entity framework.
Objects in Entity Framework are primarily assigned names using Pluralize and
Singularize. This feature is available when adding a .edmx file. Entity Framework
automatically assigns the Singular or Plural coding conventions when using this feature.
In convention names, an additional 's' is added if there is more than one record in the
object.
.NET developers are allowed to work with relational data using domain-specific objects
by object-relational mappers such as Entity Framework (EF) and Dapper. Performance-
wise, Dapper is the King of Micro ORMs.
• Dapper: A simple micro ORM, Dapper is considered a powerful system used for
data access in the .NET world. As a means to address and open-source their
issues, the Stack Overflow team created Dapper. Adding this NuGet library to
your .NET project allows you to perform database operations. In terms of speed,
it is the king of Micro ORMs and is almost as fast as using raw ADO.NET data
readers.
• Entity Framework: It is a set of .NET APIs used in software development for
performing data access. It is Microsoft's official tool for accessing data.
Comparison
POCO stands for 'Plain Old CLR Objects'. Yet, it does not mean these classes are plain or
old. A POCO class is defined as a class that contains no reference to the EF Framework
or the .NET Framework at all. In EF applications, Poco entities are known as available
domain objects.
POCO class is just like other normal .NET classes as these classes don't depend on any
framework-specific base class, unlike the standard .NET class. Persistence-ignorant
objects, or POCOs, support LINQ queries, which are supported by entities derived from
the Entity Object itself. Both EF 6 and EF Core support POCO entities.
26. In Entity Framework, what are the ways to use stored procedures?
This figure shows how stored procedure mapping details can be used in EDMX:
Database concurrency in EF means that multiple users can simultaneously modify the
same data in one database. Concurrency controls help safeguard data consistency in
situations like these.
Optimistic locking is usually used to handle database concurrency. We must first right-
click on the EDMX designer and then change the concurrency mode to Fixed in order to
implement locking. With this change, if there is a concurrency issue, we will receive a
positive concurrency exception error.
28. What are different types of loading available to load related entities
in EF?
• Eager Loading
• Lazy Loading
• Explicit Loading
29. What do you mean by lazy loading, eager loading and explicit
loading?
• Lazy Loading: This process delays the loading of related objects until they are
needed. During lazy loading, only the objects needed by the user are returned,
whereas all other related objects are only returned when needed.
• Eager Loading: This process occurs when you query for an object and all of its
related objects are returned as well. Aside from that, all related objects will load
with the parent object automatically. When the Include method is used, eager
loading can be achieved in EF6.
• Explicit Loading: Explicit loading occurs only when lazy loading is desired, even
when lazy loading is disabled. We must explicitly call the relevant load method on
the related entities to process explicit loading. When the Load method is used,
explicit loading can be achieved in EF6.
30. What are the pros and cons of different types of loading?
1. Lazy Loading
Pros
• When the relationships are not too high, use Eager Loading. So you can reduce
further queries on the server by using Eager Loading.
• If you know that related entities will be used everywhere with the main entity, use
Eager Loading.
Cons
• Adding the extra lines of code to implement lazy load makes the code more
complicated.
• It can affect a website's search engine ranking sometimes because the unloaded
content is not properly indexed.
2. Eager Loading
Pros
• Upon executing the code, the system initializes or loads the resource.
• Additionally, related entities that are referenced by a resource must be pre-
loaded.
• It is advantageous when resources need to be loaded in the background.
• It saves you time by avoiding the need to execute extra SQL queries.
Cons
• Since everything must be loaded to begin running, starting the application takes
a longer time.
• When you know you will use related entities with your main entity everywhere,
use Eager Loading.
• You should use Lazy Loading whenever you have one-to-many collections.
• Use lazy loading only if you are sure you won't need related entities right away.
• When you are unsure about whether or not an entity will be used, use explicit
loading after you have turned off Lazy Loading.
•
31. Write different types of inheritance supported by Entity Framework.
• Table per Hierarchy (TPH): The TPH inheritance representation shows one table
per inheritance hierarchy class. A discriminator column also aids in distinguishing
between inheritance classes. This is Entity Framework's default inheritance
mapping technique.
• Table per Type (TPT): In this inheritance method, each domain class has its own
table.
• Table per Concrete Class (TPC): TPC demonstrates a single table per concrete
class, but does not include the abstract class. Because of this, if an abstract class
is inherited by many concrete classes, then the tables in all those concrete classes
will have the same properties as that of an abstract class.
Complex types are defined as the non-scalar properties of entity types that assist in
organizing scalar properties within entities. In addition to scalar properties, complex
types may also have other complex type properties. Instances of complex types are
complex objects.
Rather than creating database schemas, modifying database schemas, tracking changes,
etc., Micro ORMs focus on working with database tables. EF 6.x and EF Core provide a
full set of capabilities and features, making them ORMs.
There are two types of Data Access Architecture supported by the ADO.NET Framework:
• Disconnected data access: Disconnected data access is possible with the Data
Adapter object. Datasets work independently of databases, and the data can be
edited.
• Connected data access: A Data Reader object of a Data Provider allows you to
access linked data. Data can be accessed quickly, but editing is not permitted.
35. What do you mean by SQL injection attack?
SQL injection is a method that hackers use to access sensitive information from an
organization's database. This application-layer attack is the result of inappropriate
coding in our applications, allowing hackers to inject SQL statements into your SQL
code.
The most common cause of SQL Injection is that user input fields allow SQL statements
to pass through and directly query the database. ADO.NET Data Services queries are
commonly affected by SQL Injection issues.
36. What is the best way to handle SQL injection attacks in Entity
Framework?
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.
By inserting some malicious inputs into queries and parameter names, one can generate
a SQL injection attack in Entity SQL syntax. It is best to never combine user inputs with
Entity SQL commands text to prevent or avoid this problem.
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.
38. Write the namespace that is used to include .NET Data provider for
SQL server in .NET code.
NET Data Provider for SQL Server is included in .NET code by using the namespace
System.Data.SqlClient.
Example:
You can access the entity state, as well as the current and original values of all
properties of an entity using the DbEntityEntry. EntityState can be set using the
DbEntityEntry, as shown below.
context.Entry(student).State = System.Data.Entity.EntityState.Modified;
41) Mention what is CSDL, SSDL and MSL sections in an EDMX file?
• Try to avoid to put all the DB objects into one single entity model
• Disable change tracking for entity if not needed
• Reduce response time for the first request by using pre-generating Views
• If not required try to avoid fetching all the fields
• For data manipulation select appropriate collection
• Wherever needed use compiled query
• Avoid using Views and Contains
• While binding data to grid or paging, retrieve only required no of records
• Debug and Optimize LINQ query
45) Explain how you can load related entities in EF (Entity Framework)?
• Eager Loading
• Lazy Loading
• Explicit Loading
46) Mention what is Code First approach and Model First Approach in Entity
Framework?
In Entity Framework,
• Code Approach: For code approach we avoid working with the visual
designer or entity framework.
47) Explain Lazy loading, Eager Loading, and Explicit Loading?
48) Mention what is the difference between ADO.NET and classic ADO?
49) What is the namespace used to include .NET Data provider for SQL server
in .NET code?
The namespace System.Data.SqlClient is used to include.NET data provider for SQL
server in .NET code.
50) Mention what is DataAdapter class in ADO.NET?
In ADO.NET data-adapter class fetch data from the database, stores data in a dataset
and reflects the changes made in the dataset to the database. For all type of
communication, data-adapter act as an intermediary. Using the Fill() method, data-
adapter fills data to a Data-table.
Serenity framework Interview Questions
1) What is serenity framework?
The prerequisite for installing Serenity Framework are Java SDK, Eclipse IDE,
and Selenium Driver files.
The Cucumber is an open-source tool that supports the English language specifications
required for testing. Other technical methods to use the programming languages are
.NET, Java, and other platforms. The cucumber specifications include various scenarios
and examples.
Profile in cucumber allows a way to define a group of tests in a feature file to run a
selected group instead of executing all the commands while testing a feature.
Cucumber profiles allow running step definitions and features.
8. What are the main files required to run a Cucumber test scenario?
The Two main files used to run a Cucumber test scenario are
• Step Definition
• Features
A step definition is the actual code implementation of the feature mentioned in the
feature file.
Gherkin is the language that is used by the Cucumber tool. It is a simple English
representation of the application behavior. Gherkin language uses various keywords to
define application behavior such as Feature, Given, Scenario, Scenario Outline, Then,
When, etc.
11. What is meant by a feature file?
Cucumber and Selenium are two well-known technologies. Many companies are using
Selenium for functional testing. These organizations using Selenium want to integrate
Selenium with Cucumber as it helps to read and understand the application flow.
13. What software do you need to run a Cucumber Web Test case?
The following are the software required to run a Cucumber Web Test case:
• Cucumber
• Ruby and its Development Kit
• IDE like ActiveState
• Watir (To simulate browser)
• RSpec and Ansicon (if needed)
13. Name any two build management tools that can be integrated
with Cucumber?
The two build management tools that can be integrated with cucumber are as follows:
• Maven
• Gradle
14. Mention differences between BDD and TDD?
BDD TDD
BDD is a Behavior centered development process. TDD is Test centered development process.
These tests are represented in a readable format using BDD These tests are represented using programming languages
steps. like JAVA, Ruby, etc.,
A test harness for Cucumber and RSpec enables a separate responsibility between the
context setup and interacting with the browser cleaning up the step definition files.
17. What are the two files required to run a cucumber test?
Feature file and step definition file are used to run a cucumber test.
Step definition is used to map the Test Case Steps in the feature files to code. It runs
the steps using Application Under Test (AUT) and verifies the outcomes against the
required results. To execute step definition, it must be related to the given component
in a feature.
Gherkin language is used for expressing scenario in feature files and ruby files,
including hidden automation testing for the sequence in scenarios
20. What does a cucumber features/support file contain?
Features/support file includes ruby code. These files load before the step_definitions,
which can be used for environment configuration.