EF Core Introduction šŸŽÆ

beginner
17 min

EF Core Introduction šŸŽÆ

Welcome to your journey into the world of ASP.NET and Entity Framework Core (EF Core)! In this tutorial, we'll take a deep dive into EF Core, a powerful, modern, and open-source Object-Relational Mapping (ORM) framework for .NET. Let's get started!

What is Entity Framework Core (EF Core)? šŸ“

Entity Framework Core (EF Core) is a .NET library that enables .NET developers to work with relational databases using C# and LINQ. It simplifies the process of querying and updating databases, allowing you to write code that feels like manipulating in-memory objects.

Why Use EF Core? šŸ’”

  • Simplifies Database Access: EF Core makes it easier to interact with databases, saving you from writing raw SQL queries.
  • LINQ Support: EF Core integrates with Language Integrated Query (LINQ), enabling you to query databases using C# syntax.
  • Cross-Platform: EF Core supports multiple platforms, including .NET Core, .NET 5.0, .NET 6.0, and even .NET 7.0.

Getting Started with EF Core āœ…

Before we dive into the practical examples, let's ensure you have the necessary tools installed:

  • .NET SDK: Download and install the .NET SDK from the official Microsoft website.
  • Visual Studio Code: Install Visual Studio Code, a free, open-source, and lightweight code editor.
  • EF Core Package: Install the EF Core NuGet package into your project using the dotnet CLI or Visual Studio.

Creating a Database Model šŸ“

In EF Core, a model represents the structure of your data. Here's how to create a simple model:

csharp
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; public class Student { [Key] public int Id { get; set; } [Required] [StringLength(50)] public string Name { get; set; } [Column(TypeName = "Date")] public DateTime BirthDate { get; set; } }

šŸ’” Pro Tip: The [Key] attribute makes a property the primary key, and [Required] ensures a property is always filled.

Configuring EF Core šŸ“

To use EF Core, you need to configure it in your Startup.cs file:

csharp
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); }

šŸ’” Pro Tip: The ApplicationDbContext is a class that inherits from DbContext and represents the context of your database operations.

Querying the Database šŸ’”

Now, let's create a simple example that queries the database:

csharp
public async Task<IActionResult> Index() { using (var dbContext = new ApplicationDbContext()) { var students = await dbContext.Students.ToListAsync(); return View(students); } }

In this example, we're creating an action that retrieves all students from the database and returns a view.

EF Core Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

Which attribute makes a property the primary key in EF Core?

Conclusion āœ…

In this tutorial, we've covered the basics of Entity Framework Core, including what it is, why it's useful, and how to get started with it. We've also created a simple database model and queried the database.

In the next lesson, we'll dive deeper into EF Core, exploring how to perform more complex queries and manipulate data. Stay tuned! šŸŽÆ