Are you a developer eager to streamline your data access in .NET applications? Then, you've likely encountered Entity Framework Core (EF Core), an object-database mapper that enables developers to interact with databases using LINQ queries. But where do you start, especially if you're new to EF Core? This comprehensive tutorial is designed to guide you, serving as your teacher in mastering Entity Framework Core.

Whether you're a beginner or a seasoned professional seeking to fill gaps in your knowledge, this tutorial will walk you through the intricacies of EF Core, from its installation to performing complex operations like migrations and working with related data.

Getting Started with Entity Framework Core
Before we dive into the basics, ensure Entity Framework Core is installed. If you're using .NET Core CLI, run `dotnet add package Microsoft.EntityFrameworkCore`. For .NET Core SDK, add it to your project's `csproj` file.

Now, let's create our first EF Core context and database using code-first approach.
Creating Your First Entity & Context

Start by creating entity classes representing your database tables. For instance, a simple `Blog` class might look like this:
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public List<Post> Posts { get; set; } = new();
}
Next, create your EF Core `DbContext`. Here's a basic example:
public class ApplicationDbContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
// Other DbSets...
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connection_string");
}
}
Using Relations & Migrations

Entity Framework Core supports various types of relations, like one-to-many, many-to-one, and many-to-many. To use them, you need to set up navigation properties. For a one-to-many relation, like `Posts` in the `Blog` class above, use `with` keyword to set up the inverse navigation property:
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
// Other properties...
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
Now, let's create, update, and apply migrations to create or update our database scheme.
Exploring Entity Framework Core's querying capabilities

EF Core enables querying data using LINQ. Here's how to fetch all blogs with posts ordered by post creation date:
LINQ Queries & Including Related Data









Use `Include` to fetch related data in a single query:
var blogs = await _context.Blogs
.Include(b => b.Posts)
.OrderBy(b => b.Posts.Max(p => p.CreateDate))
.ToListAsync();
Filtering & Selecting Projections
You can also filter and select specific fields using LINQ's `Where` and `Select` methods:
var blogs = await _context.Blogs
.Where(b => b.Posts.Any(p => p.CreateDate > DateTime(2021, 1, 1)))
.Select(b => new { b.Name, LatestPost = b.Posts.Max(p => p.Title) })
.ToListAsync();
Entity Framework Core's power isn't limited to what we've covered here. Dive deeper into features like change tracking, concurrency, and eager & explicit loading to truly harness its capabilities.
In the dynamic world of software development, staying updated is the key. Always keep an eye on the latest updates and roadmaps for Entity Framework Core on Microsoft's official GitHub page. Happy coding!