Logo

dev-resources.site

for different kinds of informations.

Managing Relationships, Migrations, and Performance Optimization in ASP.NET Core MVC

Published at
1/5/2025
Categories
webdev
programming
aspdotnet
microsoft
Author
sardarmudassaralikhan
Author
21 person written this
sardarmudassaralikhan
open
Managing Relationships, Migrations, and Performance Optimization in ASP.NET Core MVC

Building scalable and efficient ASP.NET Core MVC applications requires careful attention to database relationships, migrations, and performance optimization. This article covers these aspects comprehensively, guiding you through best practices and implementation strategies.

  1. Managing Relationships in ASP.NET Core MVC

Entity Framework Core (EF Core) simplifies defining and managing relationships in your database. Understanding and implementing relationships effectively ensures data integrity and simplifies querying related data.

a. One-to-One Relationship

In a one-to-one relationship, one entity is associated with exactly one other entity.

Entity Classes:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public UserProfile Profile { get; set; }
}
Enter fullscreen mode Exit fullscreen mode
public class UserProfile
{
    public int Id { get; set; }
    public string Bio { get; set; }
    public int UserId { get; set; }
    public User User { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Configuration in DbContext:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
        .HasOne(u => u.Profile)
        .WithOne(p => p.User)
        .HasForeignKey<UserProfile>(p => p.UserId);
}
Enter fullscreen mode Exit fullscreen mode

b. One-to-Many Relationship

In a one-to-many relationship, one entity is related to many others.

Entity Classes:

public class Author
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Book> Books { get; set; }
}
Enter fullscreen mode Exit fullscreen mode
public class Book
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int AuthorId { get; set; }
    public Author Author { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Configuration:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Author>()
        .HasMany(a => a.Books)
        .WithOne(b => b.Author)
        .HasForeignKey(b => b.AuthorId);
}
Enter fullscreen mode Exit fullscreen mode

c. Many-to-Many Relationship

EF Core 5.0+ supports many-to-many relationships without requiring a join entity.

Entity Classes:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Course> Courses { get; set; }
}
Enter fullscreen mode Exit fullscreen mode
public class Course
{
    public int Id { get; set; }
    public string Title { get; set; }
    public ICollection<Student> Students { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

EF Core Automatically Creates a Join Table

No additional configuration is required for basic many-to-many relationships.

  1. Migrations in ASP.NET Core MVC

Migrations are essential for evolving your database schema over time while maintaining data integrity.

a. Creating and Applying Migrations

Add a migration:

dotnet ef migrations add InitialCreate

Apply the migration:

dotnet ef database update
Enter fullscreen mode Exit fullscreen mode

b. Updating Migrations

When you modify your entity models:

Add a new migration:

dotnet ef migrations add UpdateSchema

Apply the migration:

dotnet ef database update
Enter fullscreen mode Exit fullscreen mode

c. Rolling Back Migrations

To revert to a previous migration:

dotnet ef database update PreviousMigrationName

Enter fullscreen mode Exit fullscreen mode

d. Seeding Data

Seed data in the OnModelCreating method to populate initial data.

Example:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Author>().HasData(
        new Author { Id = 1, Name = "John Doe" },
        new Author { Id = 2, Name = "Jane Smith" }
    );
}
Enter fullscreen mode Exit fullscreen mode

Run the migrations to apply the seed data.

  1. Performance Optimization

Optimizing performance ensures scalability and a better user experience. Below are strategies for improving performance in ASP.NET Core MVC.

a. Query Optimization

Use AsNoTracking for read-only queries:

var books = _context.Books.AsNoTracking().ToList();

Enter fullscreen mode Exit fullscreen mode

Use Eager Loading to fetch related data:

var author = _context.Authors.Include(a => a.Books).FirstOrDefault(a => a.Id == id);
Enter fullscreen mode Exit fullscreen mode

b. Indexing

Define indexes on frequently queried columns to improve performance:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Book>()
        .HasIndex(b => b.Title)
        .HasDatabaseName("Index_Title");
}
Enter fullscreen mode Exit fullscreen mode

c. Caching

Use caching to store frequently accessed data:

MemoryCache:

services.AddMemoryCache();

if (!_cache.TryGetValue("Books", out List<Book> books))
{
    books = _context.Books.ToList();
    _cache.Set("Books", books, TimeSpan.FromMinutes(5));
}
Enter fullscreen mode Exit fullscreen mode

d. Pagination

Fetch data in chunks using Skip and Take:

var books = _context.Books
    .Skip((pageNumber - 1) * pageSize)
    .Take(pageSize)
    .ToList();
Enter fullscreen mode Exit fullscreen mode

e. Database Connection Pooling

Enable connection pooling in the connection string:

"ConnectionStrings": {
    "DefaultConnection": "Server=.;Database=MyDb;Trusted_Connection=True;Pooling=true;"
}
Enter fullscreen mode Exit fullscreen mode

f. Profiling Tools

Use profiling tools like MiniProfiler to identify performance bottlenecks:

dotnet add package MiniProfiler.AspNetCore.Mvc

Enter fullscreen mode Exit fullscreen mode
  1. Monitoring and Diagnostics

Application Insights: Monitor application performance in production.

Logging: Implement structured logging using libraries like Serilog or NLog.

Conclusion

Managing relationships, migrations, and optimizing performance in ASP.NET Core MVC is crucial for building scalable, maintainable, and efficient applications. By leveraging EF Core for relationships, employing robust migration strategies, and implementing performance best practices, developers can create applications that are both performant and easy to maintain.

aspdotnet Article's
30 articles in total
Favicon
Uploading Files to Amazon S3 in ASP.NET Core with Razor Pages
Favicon
Managing Relationships, Migrations, and Performance Optimization in ASP.NET Core MVC
Favicon
DevExpress - Enhancing ASP.NET Web Forms with the ASPxGridView Control
Favicon
DevExpress - Simplifying Server-to-Client Data Transfer with ASPxCallback JSProperties
Favicon
🚀 [ASP.NET] How to design API allowed client specific fields?
Favicon
Web application translation in two ways.
Favicon
[Boost]
Favicon
[Boost]
Favicon
Command Pattern with Undo and Redo in C#.NET
Favicon
Server Sent Events in ASP.NET Core
Favicon
Everything New in .NET 9: The Ultimate Developer's Guide
Favicon
Is .NET 9 beneficial for Blazor?
Favicon
The Guardian Middleware: Handling Errors Globally in ASP.NET
Favicon
.NET Aspire: A Game-Changer for Cloud-Native Development Services
Favicon
Top 20+ Full-stack Projects For Beginners in C#, ASP.NET , .NET CORE
Favicon
🔐 How to Implement OAuth 2.0 Authentication in ASP.NET Core with External APIs
Favicon
Performance Optimization Techniques for ASP.NET Core Applications
Favicon
Patterns for Routing in ASP.NET Core minimal APIs
Favicon
How to prevent XSS Attacks in ASP.NET Core Web API
Favicon
MyHotixRestaurant
Favicon
Microservices using ASP.NET Core, Ocelot, MongoDB and JWT
Favicon
suggest design warehouse management
Favicon
Maximizing ASP.NET Potential through Azure Development Services
Favicon
10 Lessons I Learned from Using Aspire in Production
Favicon
Logging in ASP.NET Core
Favicon
What is ASP.NET used for in software development?
Favicon
Concepts of a Ticket in ASP.NET Identity
Favicon
Building a Scalable Multi-Tenant Community Management System with ASP.NET and Angular
Favicon
How to Manage Persistent Connections in a WhatsApp-like Chat Application Using ASP.NET SignalR and EF Core?
Favicon
Deep Dive ASP.NET Core Middleware : Part 1

Featured ones: