Skip to main content

Entity framework core

· 3 min read

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:

  1. Cascade: Deleting the principal/parent entity will cause the dependent/child entities to be deleted as well. OnDelete(DeleteBehavior.Cascade)

  2. SetNull: Deleting the principal/parent entity will set the foreign key properties in the dependent/child entities to null. OnDelete(DeleteBehavior.SetNull)

  3. SetDefault: Deleting the principal/parent entity will set the foreign key properties in the dependent/child entities to their default values. OnDelete(DeleteBehavior.SetDefault)

  4. Restrict: Prevents the deletion of the principal/parent entity if there are dependent/child entities. OnDelete(DeleteBehavior.Restrict) . An exception will be thrown.

  5. 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

  1. Indexing: Creating indexes on frequently accessed columns can improve query performance.

  2. Proper data modeling: Designing tables and relationships properly can improve query performance and prevent performance issues.

  3. Caching: Storing frequently accessed data in a cache can reduce database calls and improve application performance.

  4. Query optimization: Writing efficient queries can improve performance. Techniques such as avoiding unnecessary joins and reducing the number of returned columns can help.

  5. Connection pooling: Reusing database connections instead of creating new ones can improve performance.

  6. Batch processing: Performing multiple operations in a single database call can improve performance and reduce overhead.

  7. Asynchronous programming: Using asynchronous programming techniques can improve performance by allowing the application to continue executing while waiting for database calls to complete.