ASP.NET Hosting

ASP.NET Tutorial: Entity Framework and Entity Framework Core Understanding

Object-Relational Mappers (ORMs) like Entity Framework (EF) let.NET developers use.NET objects to interact with databases. Most of the data-access code that developers typically have to build is no longer necessary. The most recent iteration of Entity Framework, called Entity Framework Core (EF Core), is intended to be cross-platform, lightweight, and flexible.

Why Is Entity Framework Required?

  • Productivity: Less boilerplate code is needed for database operations when using EF. Developers don’t have to concentrate as much on the data access layer and more on business logic.
  • Abstraction: Developers may deal with databases without writing intricate SQL queries thanks to EF’s abstraction of the database schema.
  • Maintainability: Since data access logic is centralized with EF, it is simpler to maintain and refactor the application.
  • Support for Migrations: EF facilitates database migrations, which makes it simpler to gradually change the database schema in a controlled way.

Why It’s Named “Entity Framework Core”?

EF Core is the redesigned version of EF, built to be more modular, lightweight, and flexible. The “Core” in its name signifies its foundation on .NET Core, enabling it to run on multiple platforms (Windows, Linux, macOS) and making it suitable for cloud applications.

Key Features of Entity Framework Core

  1. Cross-Platform: Runs on .NET Core, making it usable on different operating systems.
  2. Modular and Lightweight: Provides only the necessary components for ORM and can be extended with additional features as needed.
  3. LINQ Support: Allows querying databases using LINQ (Language Integrated Query).
  4. Migrations: Supports code-first migrations to evolve the database schema.
  5. Change Tracking: Automatically keeps track of changes to entities and manages updates to the database.

Implementation in C#

Setting Up Entity Framework Core.

Install EF Core NuGet Packages

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools

Create a DbContext

using Microsoft.EntityFrameworkCore;
public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public DbSet<Customer> Customers { get; set; }
    public DbSet<Order> Orders { get; set; }
}
public class Customer
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public List<Order> Orders { get; set; }
}
public class Order
{
    public int OrderId { get; set; }
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public int CustomerId { get; set; }
    public Customer Customer { get; set; }
}

Configure DbContext in Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    }
}

AppSettings Configuration

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

CRUD Operations with Entity Framework Core
Create Operation

public async Task AddCustomerAsync(Customer customer)
{
    using (var context = new ApplicationDbContext(_options))
    {
        context.Customers.Add(customer);
        await context.SaveChangesAsync();
    }
}

Read Operation

public async Task<Customer> GetCustomerAsync(int customerId)
{
    using (var context = new ApplicationDbContext(_options))
    {
        return await context.Customers.Include(c => c.Orders)
                                      .FirstOrDefaultAsync(c => c.CustomerId == customerId);
    }
}

Update Operation

public async Task UpdateCustomerAsync(Customer customer)
{
    using (var context = new ApplicationDbContext(_options))
    {
        context.Customers.Update(customer);
        await context.SaveChangesAsync();
    }
}

Delete Operation

public async Task DeleteCustomerAsync(int customerId)
{
    using (var context = new ApplicationDbContext(_options))
    {
        var customer = await context.Customers.FindAsync(customerId);
        if (customer != null)
        {
            context.Customers.Remove(customer);
            await context.SaveChangesAsync();
        }
    }
}

Conclusion

Entity Framework and Entity Framework Core simplify database operations, making them more intuitive and maintainable for developers. By using EF Core, developers can leverage the power of ORM to build efficient, scalable, and cross-platform applications with ease.

ASP.NET Core 9 Hosting Recommendation

HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.