Entity Framework Core Crash Course | C# .NET EF Core Tutorial for Beginners (Code First)

3 min read 6 months ago
Published on Sep 02, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

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

  1. Visit the Visual Studio Code website: Download Visual Studio Code.
  2. Choose the appropriate version for your operating system and install it.

Step 2: Install .NET Core SDK

  1. Go to the .NET Core download page: Download .NET Core.
  2. Select the latest version of the .NET Core SDK and install it on your machine.

Step 3: Install Required Visual Studio Code Extensions

  1. Open Visual Studio Code.
  2. Go to the Extensions view by clicking on the Extensions icon in the sidebar or pressing Ctrl+Shift+X.
  3. Search for and install the following extensions:

Step 4: Install Entity Framework Core Packages

  1. Open a terminal in Visual Studio Code (`Ctrl + ``).
  2. Create a new project using the .NET CLI:
    dotnet new console -n BlogManager
    cd BlogManager
    
  3. 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

  1. 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; }
    }
    
  2. 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

  1. In the terminal, create a migration:
    dotnet ef migrations add InitialCreate
    
  2. Apply the migration to the database:
    dotnet ef database update
    

Step 7: Build Console Application UI

  1. Design a simple console interface to collect user input for blog posts.
  2. Use Console.ReadLine() to read user input and Console.WriteLine() for outputs.

Step 8: Implement CRUD Operations

  1. 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();
        }
    }
    
  2. Read: Implement a method to retrieve and display blog posts.
    public List<BlogPost> GetBlogPosts()
    {
        using (var db = new BlogContext())
        {
            return db.BlogPosts.ToList();
        }
    }
    
  3. 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();
        }
    }
    
  4. 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.