Entity Framework Core Crash Course | C# .NET EF Core Tutorial for Beginners (Code First)
Table of Contents
Introduction
This tutorial provides a comprehensive guide to using Entity Framework Core (EF Core) with C# and .NET, focusing on the Code First approach to building a Blog Manager console application. By following these steps, you'll learn how to set up your development environment, create models, manage migrations, and perform CRUD operations in a SQLite database.
Step 1: Install Visual Studio Code
- Visit the Visual Studio Code website: Download Visual Studio Code.
- Choose the appropriate version for your operating system and install it.
Step 2: Install .NET Core SDK
- Go to the .NET Core download page: Download .NET Core.
- Select the latest version of the .NET Core SDK and install it on your machine.
Step 3: Install Required Visual Studio Code Extensions
- Open Visual Studio Code.
- Go to the Extensions view by clicking on the Extensions icon in the sidebar or pressing
Ctrl+Shift+X
. - Search for and install the following extensions:
- SQLite3 editor: Sqlite3 Editor
- C# Dev Kit: C# Dev Kit
Step 4: Install Entity Framework Core Packages
- Open a terminal in Visual Studio Code (`Ctrl + ``).
- Create a new project using the .NET CLI:
dotnet new console -n BlogManager cd BlogManager
- Install the necessary Entity Framework packages:
dotnet add package Microsoft.EntityFrameworkCore.Design dotnet add package Microsoft.EntityFrameworkCore.Sqlite dotnet tool install --global dotnet-ef
Step 5: Create Models Using Code First Approach
- Create a new class for your model, for example,
BlogPost.cs
:public class BlogPost { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public DateTime CreatedAt { get; set; } }
- Define a
DbContext
class to manage your entities:public class BlogContext : DbContext { public DbSet<BlogPost> BlogPosts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("DataSource=blog.db"); } }
Step 6: Create and Apply Migrations
- In the terminal, create a migration:
dotnet ef migrations add InitialCreate
- Apply the migration to the database:
dotnet ef database update
Step 7: Build Console Application UI
- Design a simple console interface to collect user input for blog posts.
- Use
Console.ReadLine()
to read user input andConsole.WriteLine()
for outputs.
Step 8: Implement CRUD Operations
- Create: Add a method to add a new blog post.
public void AddBlogPost(BlogPost post) { using (var db = new BlogContext()) { db.BlogPosts.Add(post); db.SaveChanges(); } }
- Read: Implement a method to retrieve and display blog posts.
public List<BlogPost> GetBlogPosts() { using (var db = new BlogContext()) { return db.BlogPosts.ToList(); } }
- Update: Create a method to update existing blog posts.
public void UpdateBlogPost(BlogPost post) { using (var db = new BlogContext()) { db.BlogPosts.Update(post); db.SaveChanges(); } }
- Delete: Write a method to delete a blog post.
public void DeleteBlogPost(int id) { using (var db = new BlogContext()) { var post = db.BlogPosts.Find(id); if (post != null) { db.BlogPosts.Remove(post); db.SaveChanges(); } } }
Conclusion
By following these steps, you've set up a Blog Manager console application using Entity Framework Core with a Code First approach. You learned how to install necessary tools, create a SQLite database, manage migrations, and perform basic CRUD operations. As a next step, consider expanding your application by adding features like user authentication or a web interface using ASP.NET Core. For more in-depth knowledge, refer to the Entity Framework Documentation.