What’s new in Entity Framework Core 5?


Isn't it fun,

to instantly listen to a blog on the go? PLAY !

 
 

ai-Hiring-banner

Table of Content

What is Entity Framework Core?

Entity Framework Core is considered as the remake 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. Entity Framework is an enhancement to ADO.NET that provides developers an automatic mechanism for accessing & storing information within the database.

EF Core is meant to be used with .NET Core applications. However, it can also be used with standard .NET 4.5+ framework-based applications.

Entity Framework Core vs Entity Framework 6

Entity Framework Core is the new and improved version of Entity Framework for .NET Core applications. EF Core is latest, so still not as fully developed as EF 6.

EF Core continues to support below features and some concepts, similar to EF 6.

DbContext&DbSet
  1. Data Model
  2. Querying using Linq-to-Entities
  3. Change Tracking
  4. SaveChanges
  5. Migrations

EF Core will include most of the features of EF 6 in detail. However, there are somefeatures of EF 6 which aren't supported in EF Core 2.0 such as:

  1. EDMX/ Graphical Visualization of Model
  2. Entity Data Model Wizard (for DB-First approach)
  3. ObjectContext API
  4. Querying using Entity SQL.
  5. Automated Migration
  6. Inheritance: Table per type (TPT)
  7. Inheritance: Table per concrete class (TPC)
  8. Many-to-Many without join entity
  9. Entity Splitting
  10. Spatial Data
  11. Lazy loading of related data
  12. Stored procedure mapping with DbContext for CRUD operation
  13. Seed data
  14. Automatic migration
  15. EF Core includes bellow new features which aren't supported in EF 6.x:
  16. Easy relationship configuration
  17. Batch INSERT, UPDATE and DELETE operations
  18. In-memory provider for testing
  19. Support for IoC (Inversion of Control)
  20. Unique constraints
  21. Shadow properties
  22. Alternate keys
  23. Global query filter
  24. Field mapping
  25. DbContext pooling
  26. Better patterns for handling disconnected entity graphs


New features of Entity Framework Core 5

  1. Many-to-many relationship mapping
  2. Table-per-type inheritance mapping
  3. IndexAttribute to map indexes without the fluent API
  4. Database collations
  5. Filtered Include
  6. Simple logging
  7. Exclude tables from migrations
  8. Split queries for related collections
  9. Event counters
  10. SaveChanges interception and events
  11. Required 1:1 dependents
  12. Migrations scripts with transactions
  13. Rebuild SQLite tables as needed in migrations
  14. Mapping for table-valued functions
  15. DbContextFactory support for dependency injection
  16. ChangeTracker.Clear to stop tracking all entities
  17. Improved Cosmos configuration
  18. Change-tracking proxies
  19. Property bags

From the above list of features, we will learn about one of them

Many-to-many relationship mapping

A many-to-many relationship comes between two or many entities when a one-to-many relationship between one entity to many entity relations works both ways. An employee can appear in many Designationand a Designationcan include many employees. This type of relationship is shown in a database by a join table (also known as a bridging, junction, or linking table).

Initially, the join table carries just the entity key value of the relationship.Inemployees and Designationexample EmloyeeId for employee entity and DesignationId for Designationentity.

The many-to-many relationship is described in code by including the collection property in both entity Employees and Designation.

The Designationproperty is in Employee class and Employees property is in DesignationClass:

NOTE: All code snips are only to understand the many-to-many relationships from code so this code may contain only the understanding part.

public class Employees
	{
	    public intEmployeeId{ get; set; }
	    public string Name { get; set; }
	    public string City { get; set; }
	//Include Category collection using Icollection.
	    public ICollectionDesignation{ get; set; }
	}  
	public class Designation
	{
	    public int DesignationId{ get; set; }
	    public string DesignationName{ get; set; }
	//Include Employees collection using Icollection.
	    public ICollection< Employees >Employees{ get; set; }}

Searching for Dedicated ASP.Net Core Web Developer ?

Your Search ends here.


In the older version of Entity Framework Core, these two model’s definitionsare needed to describe for entity framework to implication the right type of relationship and create the join table for both of it. Entity framework core needs to describe three models for it. It is compulsory to add an entity in the model to show the join table.

public class Employees
	{
	    public int EmployeeId { get; set; }
	    public string Name { get; set; }
	    public string City { get; set; }
	    public ICollectionEmployeesDesignation{ get; set; }
	}  
	
	public class Designation
	{
	    public int DesignationId{ get; set; }
	    public string DesignationName{ get; set; }
	    public ICollectionEmployeesDesignation{ get; set; }
	}  
	
	public class EmployeesDesignation
	{
	    public int EmployeeId { get; set; }
	    public Employees Employees{ get; set; }
	    public int DesignationId{ get; set; }
	    public DesignationDesignation{ get; set; }
	}

The Join table is given the name “EmployeesDesignation” after joining two entities. Also, the relationship needs to created using the Fluent API for entity framework Core which will able to map it successfully.

protected override void ModelCreating(ModelBuildermodelBuilder)
	{
	    modelBuilder.Entity()
	.HasKey(x => new { x.EmployeeId, x.DesignationId});  
	    modelBuilder.Entity()
	.HasOne(x =>x.Employes)
	.WithMany(y => y. EmployeesDesignation)
	.HasForeignKey(x => x.EmployeeId);  
	    modelBuilder.Entity()
	.HasOne(x => x.Designation)
	.WithMany(y => y. EmployeesDesignation)
	.HasForeignKey(x => x.DesignationId);
	}

A composite key for both tables is it's the primary key, by adding both sides the many-to-many relationship are created using HasOne, with many and HasForeignKey in Fluent API methods.

This is sufficient if you want to access EmployeesDesignationdata via the Employee or Designation entities. If you want to query EmployeesDesignationdata directly, you need to also add a DbSet for joining table to the context:

public class DemoCOntex :DbContext
	{
	    public DbSetEmployes{ get; set; }
	    public DbSetDesignation{ get; set; }
	    public DbSetEmployeesDesignation{ get; set; }
	}

Conclusion

After reading the whole blog you might achieve a clear picture about the Entity Framework core and the difference between Entity framework and Entity Framework Core. We also described one of its features andhope you understand the same from this blog.

What’s new in Entity Framework Core 5?

ai-Hiring-banner

Table of Content

What is Entity Framework Core?

Entity Framework Core is considered as the remake 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. Entity Framework is an enhancement to ADO.NET that provides developers an automatic mechanism for accessing & storing information within the database.

EF Core is meant to be used with .NET Core applications. However, it can also be used with standard .NET 4.5+ framework-based applications.

Entity Framework Core vs Entity Framework 6

Entity Framework Core is the new and improved version of Entity Framework for .NET Core applications. EF Core is latest, so still not as fully developed as EF 6.

EF Core continues to support below features and some concepts, similar to EF 6.

DbContext&DbSet
  1. Data Model
  2. Querying using Linq-to-Entities
  3. Change Tracking
  4. SaveChanges
  5. Migrations

EF Core will include most of the features of EF 6 in detail. However, there are somefeatures of EF 6 which aren't supported in EF Core 2.0 such as:

  1. EDMX/ Graphical Visualization of Model
  2. Entity Data Model Wizard (for DB-First approach)
  3. ObjectContext API
  4. Querying using Entity SQL.
  5. Automated Migration
  6. Inheritance: Table per type (TPT)
  7. Inheritance: Table per concrete class (TPC)
  8. Many-to-Many without join entity
  9. Entity Splitting
  10. Spatial Data
  11. Lazy loading of related data
  12. Stored procedure mapping with DbContext for CRUD operation
  13. Seed data
  14. Automatic migration
  15. EF Core includes bellow new features which aren't supported in EF 6.x:
  16. Easy relationship configuration
  17. Batch INSERT, UPDATE and DELETE operations
  18. In-memory provider for testing
  19. Support for IoC (Inversion of Control)
  20. Unique constraints
  21. Shadow properties
  22. Alternate keys
  23. Global query filter
  24. Field mapping
  25. DbContext pooling
  26. Better patterns for handling disconnected entity graphs


New features of Entity Framework Core 5

  1. Many-to-many relationship mapping
  2. Table-per-type inheritance mapping
  3. IndexAttribute to map indexes without the fluent API
  4. Database collations
  5. Filtered Include
  6. Simple logging
  7. Exclude tables from migrations
  8. Split queries for related collections
  9. Event counters
  10. SaveChanges interception and events
  11. Required 1:1 dependents
  12. Migrations scripts with transactions
  13. Rebuild SQLite tables as needed in migrations
  14. Mapping for table-valued functions
  15. DbContextFactory support for dependency injection
  16. ChangeTracker.Clear to stop tracking all entities
  17. Improved Cosmos configuration
  18. Change-tracking proxies
  19. Property bags

From the above list of features, we will learn about one of them

Many-to-many relationship mapping

A many-to-many relationship comes between two or many entities when a one-to-many relationship between one entity to many entity relations works both ways. An employee can appear in many Designationand a Designationcan include many employees. This type of relationship is shown in a database by a join table (also known as a bridging, junction, or linking table).

Initially, the join table carries just the entity key value of the relationship.Inemployees and Designationexample EmloyeeId for employee entity and DesignationId for Designationentity.

The many-to-many relationship is described in code by including the collection property in both entity Employees and Designation.

The Designationproperty is in Employee class and Employees property is in DesignationClass:

NOTE: All code snips are only to understand the many-to-many relationships from code so this code may contain only the understanding part.

public class Employees
	{
	    public intEmployeeId{ get; set; }
	    public string Name { get; set; }
	    public string City { get; set; }
	//Include Category collection using Icollection.
	    public ICollectionDesignation{ get; set; }
	}  
	public class Designation
	{
	    public int DesignationId{ get; set; }
	    public string DesignationName{ get; set; }
	//Include Employees collection using Icollection.
	    public ICollection< Employees >Employees{ get; set; }}

Searching for Dedicated ASP.Net Core Web Developer ?

Your Search ends here.


In the older version of Entity Framework Core, these two model’s definitionsare needed to describe for entity framework to implication the right type of relationship and create the join table for both of it. Entity framework core needs to describe three models for it. It is compulsory to add an entity in the model to show the join table.

public class Employees
	{
	    public int EmployeeId { get; set; }
	    public string Name { get; set; }
	    public string City { get; set; }
	    public ICollectionEmployeesDesignation{ get; set; }
	}  
	
	public class Designation
	{
	    public int DesignationId{ get; set; }
	    public string DesignationName{ get; set; }
	    public ICollectionEmployeesDesignation{ get; set; }
	}  
	
	public class EmployeesDesignation
	{
	    public int EmployeeId { get; set; }
	    public Employees Employees{ get; set; }
	    public int DesignationId{ get; set; }
	    public DesignationDesignation{ get; set; }
	}

The Join table is given the name “EmployeesDesignation” after joining two entities. Also, the relationship needs to created using the Fluent API for entity framework Core which will able to map it successfully.

protected override void ModelCreating(ModelBuildermodelBuilder)
	{
	    modelBuilder.Entity()
	.HasKey(x => new { x.EmployeeId, x.DesignationId});  
	    modelBuilder.Entity()
	.HasOne(x =>x.Employes)
	.WithMany(y => y. EmployeesDesignation)
	.HasForeignKey(x => x.EmployeeId);  
	    modelBuilder.Entity()
	.HasOne(x => x.Designation)
	.WithMany(y => y. EmployeesDesignation)
	.HasForeignKey(x => x.DesignationId);
	}

A composite key for both tables is it's the primary key, by adding both sides the many-to-many relationship are created using HasOne, with many and HasForeignKey in Fluent API methods.

This is sufficient if you want to access EmployeesDesignationdata via the Employee or Designation entities. If you want to query EmployeesDesignationdata directly, you need to also add a DbSet for joining table to the context:

public class DemoCOntex :DbContext
	{
	    public DbSetEmployes{ get; set; }
	    public DbSetDesignation{ get; set; }
	    public DbSetEmployeesDesignation{ get; set; }
	}

Conclusion

After reading the whole blog you might achieve a clear picture about the Entity Framework core and the difference between Entity framework and Entity Framework Core. We also described one of its features andhope you understand the same from this blog.