Working with Data Using Entity Framework Core

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:02:00

Introduction to ORM and EF

Entity Framework (EF) is an Object-Relational Mapping (ORM) framework that simplifies database interactions in ASP.NET Core applications. It allows developers to work with databases using object-oriented code, abstracting away many low-level data access details.

Example

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
public class AppDbContext : DbContext
{
  public DbSet<Student> Students { get; set; }
}
public class Student
{
  public int Id { get; set; }
  public string Name { get; set; }
}
public static void Main()
{
  using var context = new AppDbContext();
  var newStudent = new Student { Name = "John Doe" };
  context.Students.Add(newStudent);
  context.SaveChanges();
  var student = context.Students.First();
  Console.WriteLine($"Student ID: {student.Id}, Name: {student.Name}");
}

Introduction to EF Core

Entity Framework Core (EF Core) is a lightweight, cross-platform version of EF. It provides a modern and flexible ORM framework for ASP.NET Core applications, allowing easy data access and manipulation.

Example

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
public class AppDbContext : DbContext
{
  public DbSet<Product> Products { get; set; }
}
public class Product
{
  public int Id { get; set; }
  public string Name { get; set; }
  public decimal Price { get; set; }
}
public static void Main()
{
  using var context = new AppDbContext();
  var products = context.Products.Where(p => p.Price > 50).ToList();
  foreach (var product in products)
  {
    Console.WriteLine($"Product ID: {product.Id}, Name: {product.Name}, Price: {product.Price:C}");
  }
}

Creating Entities and Database

In EF Core, you define entity classes to represent your data model. These entities are used to create a database schema and interact with data.

Example

// Define entity class
public class Author
{
  public int AuthorId { get; set; }
  public string Name { get; set; }
}
// In the application
using var context = new AppDbContext();
context.Database.EnsureCreated(); // Create the database

Performing CRUD Operations

CRUD (Create, Read, Update, Delete) operations in EF Core involve adding, querying, modifying, and deleting data in the database using C# code.

Example

// Create
var newAuthor = new Author { Name = "J.K. Rowling" };
context.Authors.Add(newAuthor);
context.SaveChanges();
// Read
var author = context.Authors.FirstOrDefault(a => a.Name == "J.K. Rowling");
// Update
if (author != null)
{
  author.Name = "Joanne Rowling";
  context.SaveChanges();
}
// Delete
if (author != null)
{
  context.Authors.Remove(author);
  context.SaveChanges();
}

EF Core Migration

EF Core provides a mechanism for managing database schema changes using migrations. Migrations allow developers to evolve the database schema in a controlled and structured manner.

Example

# In the terminal
dotnet ef migrations add InitialCreate
dotnet ef database update

Performing CRUD Operations using ASP.NET Core

In ASP.NET Core, you can integrate EF Core to perform CRUD operations on a database and expose data to web APIs or web pages. This enables the building of data-driven web applications.

Example

// In an ASP.NET Core Controller
public class AuthorController : ControllerBase
{
  private readonly AppDbContext _context;
  public AuthorController(AppDbContext context)
  {
    _context = context;
  }
  [HttpGet]
  public IActionResult GetAuthors()
  {
    var authors = _context.Authors.ToList();
    return Ok(authors);
  }
  // Implement Create, Update, and Delete actions similarly
}
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this