The Entity Framework (EF) is a convenient way to interact with databases in .NET applications, reducing the amount of code needed to retrieve and manipulate data. In this post, we'll explore several examples of using Entity Framework in C#, covering both Entity Framework 6 (EF6) and its successor, Entity Framework Core (EF Core).

By the end of this guide, you'll have a solid understanding of how to use Entity Framework to perform common database operations, from retrieving and modifying data to creating and deleting records. Let's dive into the examples, starting with EF Core, the recommended version for new projects.

Entity Framework Core (EF Core) Examples
EF Core is the cross-platform version of Entity Framework, designed to be more lightweight and fast. It's ideal for mobile, desktop, and cloud applications. Here are two essential examples to get you started with EF Core.

First, let's assume you have the following `Blog` and `Post` models:
```csharp
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public ListRetrieving Data with EF Core

To fetch data, inject the `DbContext` into your controller or service and use the `DbSet` to query the database:
```csharp var blogs = await _context.Blogs.ToListAsync(); var blog = await _context.Blogs.FindAsync(1); // Retrieve a single entity by integer id var posts = await _context.Posts.ToListAsync(); ```
Adding, Updating, and Deleting Data with EF Core
EF Core supports adding, updating, and deleting entities using the `Add`, `Remove`, and LINQ-based methods. Here's how you can perform CRUD operations:

```csharp var newBlog = new Blog { Name = "Blogging with EF Core" }; _context.Blogs.Add(newBlog); await _context.SaveChangesAsync(); var blog = await _context.Blogs.FindAsync(1); blog.Name = "Updated Blog Name"; await _context.SaveChangesAsync(); var post = await _context.Posts.FindAsync(1); _context.Posts.Remove(post); await _context.SaveChangesAsync(); ```
Entity Framework 6 (EF6) Examples
While EF Core is the recommended version, some legacy projects still use Entity Framework 6. Let's examine two examples showcasing basic EF6 usage.
Assuming you have the same `Blog` and `Post` models from the previous examples, and a `DbContext` derived from ` DbContext`:

```csharp
public class MyDbContext : DbContext
{
public DbSetRetrieving Data with EF6
In EF6, use LINQ to query the `DbSet`, and call `ToList()` or `FirstOrDefault()` to execute the query:









```csharp var blogs = _context.Blogs.ToList(); var blog = _context.Blogs.Find(1); // Retrieve a single entity by integer id var posts = _context.Posts.ToList(); ```
Adding, Updating, and Deleting Data with EF6
Like EF Core, EF6 supports adding, updating, and deleting entities using standard LINQ methods. Here's how you can perform CRUD operations:
```csharp var newBlog = new Blog { Name = "Blogging with EF6" }; _context.Blogs.Add(newBlog); _context.SaveChanges(); var blog = _context.Blogs.Find(1); blog.Name = "Updated Blog Name"; _context.SaveChanges(); var post = _context.Posts.Find(1); _context.Posts.Remove(post); _context.SaveChanges(); ```
Both EF Core and EF6 provide convenient ways to interact with databases in your C# applications. Understanding these examples will help you master Entity Framework and build powerful data-driven applications.
Now that you've wrapped your head around Entity Framework examples in C#, it's time to practice and explore more advanced features, such as migrations, eager loading, and lazy loading. Happy coding!