Introduction
- Entity Framework Core is an ORM framework, open-source, lightweight and cross-platform developed by Microsoft.
- It enables developers to work with databases using .NET object and EF Core is built on top of ADO.NET
Relationship
One-to-One Relationship
modelBuilder.Entity<Author>()
.HasOne(a => a.Book) // Author has one Book
.WithOne(b => b.Author) // Book has one Author
.HasForeignKey<Book>(b => b.AuthorId); // Foreign key in Book referencing AuthorId
One-to-Many Relationship
modelBuilder.Entity<Author>()
.HasMany(a => a.Books) // Author has many Books
.WithOne(b => b.Author) // Book has one Author
.HasForeignKey(b => b.AuthorId); // Foreign key in Book referencing AuthorId
Many-to-Many Relationship
modelBuilder.Entity<StudentCourse>()
.HasKey(sc => new { sc.StudentId, sc.CourseId }); // Define composite primary key for the join table
modelBuilder.Entity<StudentCourse>()
.HasOne(sc => sc.Student)
.WithMany(s => s.Courses)
.HasForeignKey(sc => sc.StudentId); // Foreign key in join table referencing StudentId
modelBuilder.Entity<StudentCourse>()
.HasOne(sc => sc.Course)
.WithMany(c => c.Students)
.HasForeignKey(sc => sc.CourseId); // Foreign key in join table referencing CourseId
DeleteBehavior
The DeleteBehavior
enum in EF Core includes the following options:
Cascade: Deleting the principal/parent entity will cause the dependent/child entities to be deleted as well.
OnDelete(DeleteBehavior.Cascade)
SetNull: Deleting the principal/parent entity will set the foreign key properties in the dependent/child entities to null.
OnDelete(DeleteBehavior.SetNull)
SetDefault: Deleting the principal/parent entity will set the foreign key properties in the dependent/child entities to their default values.
OnDelete(DeleteBehavior.SetDefault)
Restrict: Prevents the deletion of the principal/parent entity if there are dependent/child entities.
OnDelete(DeleteBehavior.Restrict)
. An exception will be thrown.NoAction: Similar to
Restrict
, it is used to specify no action on delete.OnDelete(DeleteBehavior.NoAction)
and you'll need to handle constraints in your application logic.
For example:
modelBuilder.Entity<ParentEntity>()
.HasMany(p => p.ChildEntities)
.WithOne(c => c.ParentEntity)
.OnDelete(DeleteBehavior.Restrict);
This configuration would set the delete behavior for the relationship between ParentEntity
and ChildEntity
to Restrict
.
Best pratices
Indexing: Creating indexes on frequently accessed columns can improve query performance.
Proper data modeling: Designing tables and relationships properly can improve query performance and prevent performance issues.
Caching: Storing frequently accessed data in a cache can reduce database calls and improve application performance.
Query optimization: Writing efficient queries can improve performance. Techniques such as avoiding unnecessary joins and reducing the number of returned columns can help.
Connection pooling: Reusing database connections instead of creating new ones can improve performance.
Batch processing: Performing multiple operations in a single database call can improve performance and reduce overhead.
Asynchronous programming: Using asynchronous programming techniques can improve performance by allowing the application to continue executing while waiting for database calls to complete.