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- Data Model
- Querying using Linq-to-Entities
- Change Tracking
- SaveChanges
- 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:
- EDMX/ Graphical Visualization of Model
- Entity Data Model Wizard (for DB-First approach)
- ObjectContext API
- Querying using Entity SQL.
- Automated Migration
- Inheritance: Table per type (TPT)
- Inheritance: Table per concrete class (TPC)
- Many-to-Many without join entity
- Entity Splitting
- Spatial Data
- Lazy loading of related data
- Stored procedure mapping with DbContext for CRUD operation
- Seed data
- Automatic migration
- EF Core includes bellow new features which aren't supported in EF 6.x:
- Easy relationship configuration
- Batch INSERT, UPDATE and DELETE operations
- In-memory provider for testing
- Support for IoC (Inversion of Control)
- Unique constraints
- Shadow properties
- Alternate keys
- Global query filter
- Field mapping
- DbContext pooling
- Better patterns for handling disconnected entity graphs
New features of Entity Framework Core 5
- Many-to-many relationship mapping
- Table-per-type inheritance mapping
- IndexAttribute to map indexes without the fluent API
- Database collations
- Filtered Include
- Simple logging
- Exclude tables from migrations
- Split queries for related collections
- Event counters
- SaveChanges interception and events
- Required 1:1 dependents
- Migrations scripts with transactions
- Rebuild SQLite tables as needed in migrations
- Mapping for table-valued functions
- DbContextFactory support for dependency injection
- ChangeTracker.Clear to stop tracking all entities
- Improved Cosmos configuration
- Change-tracking proxies
- 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.