0
0
CsharpConceptBeginner · 4 min read

What is Entity Framework in C#: Overview and Example

Entity Framework is an object-relational mapper (ORM) for C# that helps developers work with databases using .NET objects instead of SQL queries. It simplifies data access by automatically translating your C# classes into database tables and handling data operations behind the scenes.
⚙️

How It Works

Entity Framework acts like a translator between your C# code and the database. Imagine you have a spreadsheet (the database) and you want to work with it using your own custom forms (C# classes). Instead of writing complicated instructions to update the spreadsheet, Entity Framework lets you use simple commands on your forms, and it takes care of updating the spreadsheet correctly.

It tracks changes you make to your objects and then generates the right SQL commands to save those changes to the database. This way, you focus on your application logic, and Entity Framework handles the data storage details.

💻

Example

This example shows how to define a simple model and save a new record to a database using Entity Framework Core.

csharp
using System;
using Microsoft.EntityFrameworkCore;

// Define a model class
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

// Define a database context
public class ShopContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        options.UseInMemoryDatabase("ShopDB");
    }
}

class Program
{
    static void Main()
    {
        using var context = new ShopContext();

        // Create a new product
        var product = new Product { Name = "Coffee", Price = 4.99m };

        // Add product to the database
        context.Products.Add(product);
        context.SaveChanges();

        // Retrieve and print all products
        foreach (var p in context.Products)
        {
            Console.WriteLine($"Product: {p.Name}, Price: {p.Price}");
        }
    }
}
Output
Product: Coffee, Price: 4.99
🎯

When to Use

Use Entity Framework when you want to simplify database operations in your C# applications without writing raw SQL. It is great for applications that need to store, retrieve, and update data in relational databases like SQL Server, SQLite, or PostgreSQL.

It fits well in web apps, desktop apps, and services where you want to focus on business logic rather than database details. For example, an online store, a blog platform, or a customer management system can benefit from Entity Framework.

Key Points

  • Entity Framework maps C# classes to database tables automatically.
  • It tracks changes and generates SQL commands for you.
  • Supports multiple database providers like SQL Server and SQLite.
  • Helps write cleaner, easier-to-maintain data access code.
  • Works well with modern .NET applications using Entity Framework Core.

Key Takeaways

Entity Framework lets you work with databases using C# objects instead of SQL queries.
It automatically handles data storage and retrieval, saving you from writing raw SQL.
Use it to simplify data access in web, desktop, or service applications.
Entity Framework Core supports many databases and modern .NET projects.
It improves code readability and maintainability by abstracting database details.