DEV Community

Sardar Mudassar Ali Khan
Sardar Mudassar Ali Khan

Posted on

What is the entity framework? with Example

Entity Framework is an object-relational mapping (ORM) framework provided by Microsoft. It simplifies the process of working with relational databases by allowing developers to work with database data as objects in their code.

Entity Framework provides a set of tools and libraries that enable developers to create, read, update, and delete data from a database without having to write complex SQL queries. It abstracts the underlying database schema and allows developers to interact with the database using a high-level object-oriented approach.

Here's a simple example to illustrate how Entity Framework works:

Let's say you have a database with a table called "Customers" that has columns like "Id", "Name", and "Email". Using Entity Framework, you can define a corresponding C# class called "Customer" that represents the structure of the table:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Next, you would create a DbContext class, which acts as a bridge between your code and the database. The DbContext represents the database context and provides a way to query and interact with the database entities. Here's an example:

public class CustomerContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the DbSet property represents the "Customers" table in the database.

To perform database operations, you can create an instance of the CustomerContext and use it to interact with the Customer entities:

using (var context = new CustomerContext())
{
    // Insert a new customer
    var newCustomer = new Customer
    {
        Name = "John Doe",
        Email = "johndoe@example.com"
    };
    context.Customers.Add(newCustomer);
    context.SaveChanges();

    // Retrieve customers
    var customers = context.Customers.ToList();
    foreach (var customer in customers)
    {
        Console.WriteLine($"Id: {customer.Id}, Name: {customer.Name}, Email: {customer.Email}");
    }

    // Update a customer
    var existingCustomer = context.Customers.FirstOrDefault(c => c.Id == 1);
    if (existingCustomer != null)
    {
        existingCustomer.Email = "updatedemail@example.com";
        context.SaveChanges();
    }

    // Delete a customer
    var customerToDelete = context.Customers.FirstOrDefault(c => c.Id == 2);
    if (customerToDelete != null)
    {
        context.Customers.Remove(customerToDelete);
        context.SaveChanges();
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the code creates a new customer, inserts it into the database, retrieves all customers, updates an existing customer's email, and deletes a customer from the database.

Entity Framework handles the mapping between the C# objects and the database tables, as well as the generation of SQL queries necessary to perform the requested operations. It provides a convenient and intuitive way to work with databases, abstracting away the complexities of database access.

Top comments (0)