Entity Core with
Core Microservices
Anas Siddiqui
Advait Ashutosh Narkar
Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
 Punctuality
Join the session 5 minutes prior to the session start time. We start on
time and conclude on time!
 Feedback
Make sure to submit a constructive feedback for all sessions as it is very
helpful for the presenter.
 Silent Mode
Keep your mobile devices in silent mode, feel free to move out of session
in case you need to attend an urgent call.
 Avoid Disturbance
Avoid unwanted chit chat during the session.
1. Introduction
2. Entity Framework Architecture
3. ORM
4. EF Core vs EF6
5. Models
6. DbContext
7. Querying Data
8. EF Core in Microservices
9. Conclusion
Introduction
 Entity Framework Core is the new version of Entity Framework after EF 6.x. It is open-source,
lightweight, extensible and a cross-platform version of Entity Framework data access technology.
 Entity Framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to
ADO.NET that gives developers an automated mechanism for accessing & storing the data in the
database
 EF Core is intended to be used with .NET Core applications. However, it can also be used with
standard .NET 4.5+ framework-based applications.
 It saves development time, reduces the need for repetitive data access code, promotes cleaner
code architecture, supports various database providers.
 Entity Framework supports various database providers, including SQL Server, MySQL,
PostgreSQL, SQLite, and more, allowing developers to choose the database that best suits their
application requirements without changing the application code.
Entity Framework Architecture
 DbContext:
− Main interface for database interactions.
− Manages connections, transactions, and
caching.
 Entities:
− Plain .NET classes representing database
data.
− No base class or interface required.
 DbSet:
− Collection interface for entity manipulation.
− Allows querying and CRUD operations.
 Change Tracking:
− Automatic tracking of entity changes.
− Facilitates SQL statement generation.
Entity Framework Core follows a modular and lightweight architecture designed to support a wide range of
.NET applications while providing improved performance and flexibility. Here's an overview of its key
components:
 Querying:
− LINQ and raw SQL query support.
− Translates LINQ queries into SQL.
 Database Providers:
− Supports various database engines.
− Each provider implements database-specific
features.
 Configuration:
− Fluent API or annotations for customization.
− Configures mappings, relationships, and more.
− Facilitates SQL statement generation.
Object-Relational Mapping
(ORM)
 ORM is a programming technique that
enables developers to map objects from the
application domain to relational database
tables and vice versa. Entity Framework
implements ORM by generating SQL queries
based on LINQ queries or method calls on
DbSet objects, allowing developers to work
with objects instead of database tables
directly.
 Benefits of ORM: Reduces the need for
manual SQL queries, promotes code
reusability, improves maintainability, abstracts
database-specific implementations.
EF Core vs EF6
 Cross-platform Compatibility:
− EF Core is designed to work with .NET Core, .NET 5+, and .NET Framework, making it suitable for
cross-platform development across Windows, Linux, and macOS. This allows for greater flexibility in
choosing deployment platforms and enables modern cloud-native application development.
 Improved Performance:
− EF Core offers better performance compared to EF6, with optimizations in query execution, change
tracking, and database interactions. It includes features such as batched updates and eager loading
enhancements, resulting in faster data access operations and reduced latency.
 Modular Architecture:
− EF Core has a more modular and lightweight architecture compared to EF6, making it easier to
maintain, extend, and customize. It allows developers to include only the components they need,
reducing the overhead of unused features and dependencies.
Advantages of using Entity Framework Core (EF Core) over Entity Framework 6 (EF6):
 Provider Model:
EF Core introduces a provider model that allows for easier integration with different database providers,
enabling support for a wide range of databases beyond SQL Server. This makes it easier to switch
between database providers or support multiple databases within the same application.
 Command-line Tools:
EF Core provides command-line tools (dotnet ef) for migrations, database updates, and scaffolding,
making it easier to manage database schema changes and generate code. These tools streamline the
development workflow and provide consistency across different development environments.
 Integration with .NET Core and .NET 5+:
EF Core is tightly integrated with .NET Core and the latest .NET 5+ versions, providing support for
modern development scenarios such as microservices, containers, and serverless architectures. It
leverages the latest features and improvements in the .NET ecosystem, offering a more seamless
development experience.
 Cloud-native Capabilities:
EF Core is well-suited for cloud-native application development, with features such as better support
for asynchronous operations, lightweight deployment, and improved scalability. It enables developers to
build modern, cloud-ready applications that can leverage the benefits of cloud platforms such as Azure
and AWS.
EF Core Development approaches
 EF Core supports two development approaches
1) Code-First
2) Database-First.
 In the code-first approach, EF Core API creates the database and tables using migration based on the
conventions and configuration provided in your domain classes. This approach is useful in Domain
Driven Design (DDD).
 In the database-first approach, EF Core API creates the domain and context classes based on your
existing database using EF Core commands. This has limited support in EF Core as it does not
support visual designer or wizard.
Models
 Models in Entity Framework represent the structure of the data that the application works with. They
are represented by plain .NET classes that typically correspond to tables in the database.
 It define the properties and relationships between entities, providing a clear representation of the
application's domain.
 It encapsulate the business logic and data access logic within the application, enabling developers to
work with data in an object-oriented manner.
 Example: An e-commerce application might have models representing entities such as Product, Order,
and Customer, with properties like Name, Price, and Quantity.
DbContext
 DbContext is the primary class responsible for interacting with the database in Entity Framework. It
represents a session with the database and manages database connections, transactions, and
caching.
 Key Features:
− DbSet: A DbSet<TEntity> property on the DbContext represents a collection of entities of type
TEntity. It enables CRUD operations and LINQ queries against the corresponding database table.
− Change Tracking: DbContext automatically tracks changes made to entities within its scope,
allowing it to generate SQL statements to persist those changes to the database during
SaveChanges().
− Configuration: DbContext provides methods for configuring various aspects of its behavior,
including entity mappings, relationships, and database connection settings.
 Usage: Developers create a custom subclass of DbContext for each database context in their
application. They define DbSet properties to represent entity collections and override DbContext
methods to configure its behavior.
Querying Data
 Querying with LINQ:
− Definition: LINQ (Language Integrated Query) provides a powerful and expressive syntax for
querying data in Entity Framework. Developers can write LINQ queries against DbSet properties
of the DbContext to retrieve data from the underlying database.
− Benefits: LINQ queries are type-safe, compile-time checked, and provide IntelliSense support,
making them easier to write and maintain compared to raw SQL queries.
− Example: var products = dbContext.Products.Where(p => p.Category ==
"Electronics").ToList();
 Querying with SQL:
− Definition: Entity Framework allows developers to execute raw SQL queries against the database
when more complex or performance-critical operations are required. Raw SQL queries can be
executed using methods like FromSql() on DbSet properties.
− Benefits: Raw SQL queries provide flexibility and control over the SQL code executed against the
database, allowing developers to optimize performance and leverage database-specific features.
− Example: var products = dbContext.Products.FromSql("SELECT * FROM Products WHERE
Category = 'Electronics'").ToList();
EF Core in Microservices
 Consistent Data Access Across Microservices:
− EF Core provides a unified API for data access, regardless of the underlying database technology.
− In a microservices ecosystem, where each service may use different databases, EF Core ensures a
consistent approach to querying and updating data.
 Domain-Driven Design (DDD) Integration:
− EF Core supports POCO (Plain Old CLR Object) entities, aligning well with DDD principles.
− By encapsulating domain behaviour within entities, you maintain a clear separation between business logic
and persistence concerns.
 Scalability and Performance Optimization:
− EF Core allows fine-tuning of queries, enabling efficient data retrieval.
− As your microservices scale, EF Core adapts to handle increased load while maintaining performance.
 Code-First Approach and Migrations:
− With EF Core’s code-first approach, you define your domain model in C# classes.
− Migrations automate database schema changes, making it easy to evolve your microservice without manual
SQL scripts.
Conclusion
Entity Framework Core into microservices architecture offers numerous advantages, including simplified
development, cross-platform compatibility, performance optimization, scalability, maintainability, seamless
integration with .NET ecosystem, and robust community support. Leveraging EF Core empowers
organizations to build resilient, scalable, and efficient microservices solutions that meet evolving business
needs and drive innovation.
Entity Core with Core Microservices.pptx
Entity Core with Core Microservices.pptx

Entity Core with Core Microservices.pptx

  • 1.
    Entity Core with CoreMicroservices Anas Siddiqui Advait Ashutosh Narkar
  • 2.
    Lack of etiquetteand manners is a huge turn off. KnolX Etiquettes  Punctuality Join the session 5 minutes prior to the session start time. We start on time and conclude on time!  Feedback Make sure to submit a constructive feedback for all sessions as it is very helpful for the presenter.  Silent Mode Keep your mobile devices in silent mode, feel free to move out of session in case you need to attend an urgent call.  Avoid Disturbance Avoid unwanted chit chat during the session.
  • 3.
    1. Introduction 2. EntityFramework Architecture 3. ORM 4. EF Core vs EF6 5. Models 6. DbContext 7. Querying Data 8. EF Core in Microservices 9. Conclusion
  • 5.
    Introduction  Entity FrameworkCore is the new version of Entity Framework after EF 6.x. It is open-source, lightweight, extensible and a cross-platform version of Entity Framework data access technology.  Entity Framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database  EF Core is intended to be used with .NET Core applications. However, it can also be used with standard .NET 4.5+ framework-based applications.  It saves development time, reduces the need for repetitive data access code, promotes cleaner code architecture, supports various database providers.  Entity Framework supports various database providers, including SQL Server, MySQL, PostgreSQL, SQLite, and more, allowing developers to choose the database that best suits their application requirements without changing the application code.
  • 6.
    Entity Framework Architecture DbContext: − Main interface for database interactions. − Manages connections, transactions, and caching.  Entities: − Plain .NET classes representing database data. − No base class or interface required.  DbSet: − Collection interface for entity manipulation. − Allows querying and CRUD operations.  Change Tracking: − Automatic tracking of entity changes. − Facilitates SQL statement generation. Entity Framework Core follows a modular and lightweight architecture designed to support a wide range of .NET applications while providing improved performance and flexibility. Here's an overview of its key components:  Querying: − LINQ and raw SQL query support. − Translates LINQ queries into SQL.  Database Providers: − Supports various database engines. − Each provider implements database-specific features.  Configuration: − Fluent API or annotations for customization. − Configures mappings, relationships, and more. − Facilitates SQL statement generation.
  • 7.
    Object-Relational Mapping (ORM)  ORMis a programming technique that enables developers to map objects from the application domain to relational database tables and vice versa. Entity Framework implements ORM by generating SQL queries based on LINQ queries or method calls on DbSet objects, allowing developers to work with objects instead of database tables directly.  Benefits of ORM: Reduces the need for manual SQL queries, promotes code reusability, improves maintainability, abstracts database-specific implementations.
  • 8.
    EF Core vsEF6  Cross-platform Compatibility: − EF Core is designed to work with .NET Core, .NET 5+, and .NET Framework, making it suitable for cross-platform development across Windows, Linux, and macOS. This allows for greater flexibility in choosing deployment platforms and enables modern cloud-native application development.  Improved Performance: − EF Core offers better performance compared to EF6, with optimizations in query execution, change tracking, and database interactions. It includes features such as batched updates and eager loading enhancements, resulting in faster data access operations and reduced latency.  Modular Architecture: − EF Core has a more modular and lightweight architecture compared to EF6, making it easier to maintain, extend, and customize. It allows developers to include only the components they need, reducing the overhead of unused features and dependencies. Advantages of using Entity Framework Core (EF Core) over Entity Framework 6 (EF6):
  • 9.
     Provider Model: EFCore introduces a provider model that allows for easier integration with different database providers, enabling support for a wide range of databases beyond SQL Server. This makes it easier to switch between database providers or support multiple databases within the same application.  Command-line Tools: EF Core provides command-line tools (dotnet ef) for migrations, database updates, and scaffolding, making it easier to manage database schema changes and generate code. These tools streamline the development workflow and provide consistency across different development environments.  Integration with .NET Core and .NET 5+: EF Core is tightly integrated with .NET Core and the latest .NET 5+ versions, providing support for modern development scenarios such as microservices, containers, and serverless architectures. It leverages the latest features and improvements in the .NET ecosystem, offering a more seamless development experience.  Cloud-native Capabilities: EF Core is well-suited for cloud-native application development, with features such as better support for asynchronous operations, lightweight deployment, and improved scalability. It enables developers to build modern, cloud-ready applications that can leverage the benefits of cloud platforms such as Azure and AWS.
  • 10.
    EF Core Developmentapproaches  EF Core supports two development approaches 1) Code-First 2) Database-First.  In the code-first approach, EF Core API creates the database and tables using migration based on the conventions and configuration provided in your domain classes. This approach is useful in Domain Driven Design (DDD).  In the database-first approach, EF Core API creates the domain and context classes based on your existing database using EF Core commands. This has limited support in EF Core as it does not support visual designer or wizard.
  • 12.
    Models  Models inEntity Framework represent the structure of the data that the application works with. They are represented by plain .NET classes that typically correspond to tables in the database.  It define the properties and relationships between entities, providing a clear representation of the application's domain.  It encapsulate the business logic and data access logic within the application, enabling developers to work with data in an object-oriented manner.  Example: An e-commerce application might have models representing entities such as Product, Order, and Customer, with properties like Name, Price, and Quantity.
  • 13.
    DbContext  DbContext isthe primary class responsible for interacting with the database in Entity Framework. It represents a session with the database and manages database connections, transactions, and caching.  Key Features: − DbSet: A DbSet<TEntity> property on the DbContext represents a collection of entities of type TEntity. It enables CRUD operations and LINQ queries against the corresponding database table. − Change Tracking: DbContext automatically tracks changes made to entities within its scope, allowing it to generate SQL statements to persist those changes to the database during SaveChanges(). − Configuration: DbContext provides methods for configuring various aspects of its behavior, including entity mappings, relationships, and database connection settings.  Usage: Developers create a custom subclass of DbContext for each database context in their application. They define DbSet properties to represent entity collections and override DbContext methods to configure its behavior.
  • 15.
    Querying Data  Queryingwith LINQ: − Definition: LINQ (Language Integrated Query) provides a powerful and expressive syntax for querying data in Entity Framework. Developers can write LINQ queries against DbSet properties of the DbContext to retrieve data from the underlying database. − Benefits: LINQ queries are type-safe, compile-time checked, and provide IntelliSense support, making them easier to write and maintain compared to raw SQL queries. − Example: var products = dbContext.Products.Where(p => p.Category == "Electronics").ToList();  Querying with SQL: − Definition: Entity Framework allows developers to execute raw SQL queries against the database when more complex or performance-critical operations are required. Raw SQL queries can be executed using methods like FromSql() on DbSet properties. − Benefits: Raw SQL queries provide flexibility and control over the SQL code executed against the database, allowing developers to optimize performance and leverage database-specific features. − Example: var products = dbContext.Products.FromSql("SELECT * FROM Products WHERE Category = 'Electronics'").ToList();
  • 16.
    EF Core inMicroservices  Consistent Data Access Across Microservices: − EF Core provides a unified API for data access, regardless of the underlying database technology. − In a microservices ecosystem, where each service may use different databases, EF Core ensures a consistent approach to querying and updating data.  Domain-Driven Design (DDD) Integration: − EF Core supports POCO (Plain Old CLR Object) entities, aligning well with DDD principles. − By encapsulating domain behaviour within entities, you maintain a clear separation between business logic and persistence concerns.  Scalability and Performance Optimization: − EF Core allows fine-tuning of queries, enabling efficient data retrieval. − As your microservices scale, EF Core adapts to handle increased load while maintaining performance.  Code-First Approach and Migrations: − With EF Core’s code-first approach, you define your domain model in C# classes. − Migrations automate database schema changes, making it easy to evolve your microservice without manual SQL scripts.
  • 17.
    Conclusion Entity Framework Coreinto microservices architecture offers numerous advantages, including simplified development, cross-platform compatibility, performance optimization, scalability, maintainability, seamless integration with .NET ecosystem, and robust community support. Leveraging EF Core empowers organizations to build resilient, scalable, and efficient microservices solutions that meet evolving business needs and drive innovation.