Code-First Development in EF Core

Introduction:

Entity Framework (EF) Core is a popular Object-Relational Mapping (ORM) framework used for accessing databases in .NET applications. EF Core provides a flexible and easy-to-use API for performing database operations such as querying, updating, and deleting data.

One of the key features of EF Core is code-first development. In this approach, you create the domain model classes and EF Core automatically generates the database schema based on the classes.

In this article, we will explore how to get started with code-first development in EF Core.

Step 1: Installing EF Core

The first step is to install EF Core in your .NET project. You can do this using the NuGet package manager in Visual Studio or by using the command line:

dotnet add package Microsoft.EntityFrameworkCore

Step 2: Creating the Domain Model

Once you have installed EF Core, you need to create the domain model classes that represent the data you want to store in the database.

Here's an example of a simple domain model that represents a blog post:

public class BlogPost
{
   public int Id { get; set; }
   public string Title { get; set; }
   public string Content { get; set; }
   public DateTime PublishedDate { get; set; }
}

In the example above, we have created a class called "BlogPost" that has four properties: "Id", "Title", "Content", and "PublishedDate". The "Id" property is marked with the "Key" attribute to indicate that it is the primary key of the table.

Step 3: Creating the DbContext

The next step is to create a DbContext class that will represent the database context for your application. The DbContext class is responsible for managing database connections, executing queries, and tracking changes to the database.

Here's an example of a DbContext class that includes a DbSet for the "BlogPost" class:

public class MyDbContext : DbContext
{
   public MyDbContext(DbContextOptions<MyDbContext> options)
      : base(options)
   { }

   public DbSet<BlogPost> BlogPosts { get; set; }
}

In the example above, we have created a class called "MyDbContext" that inherits from the EF Core DbContext class. The constructor takes a "DbContextOptions" object that is used to configure the database connection. We have also added a DbSet property for the "BlogPost" class, which allows us to perform CRUD operations on the "BlogPosts" table.

Step 4: Creating the Database

Now that we have created the domain model and DbContext, we can use EF Core to create the database schema based on the classes.

Here's an example of how to create the database schema using EF Core:

using (var context = new MyDbContext(options))
{
   context.Database.EnsureCreated();
}

In the example above, we create a new instance of the "MyDbContext" class and call the "EnsureCreated" method to create the database schema based on the domain model.

Step 5: Using the DbContext in Code

Now that we have created the database schema, we can use the DbContext to perform CRUD operations on the database. Here's an example of how to add a new blog post to the database:

using (var context = new MyDbContext(options))
{
   var blogPost = new BlogPost
   {
      Title = "My First Blog Post",
      Content = "Hello World!",
      PublishedDate = DateTime.Now
   };

   context.BlogPosts.Add(blogPost);
   context.SaveChanges();
}

In the example above, we create a new instance of the "BlogPost" class and set the "Title", "Content", and "Published".

Related posts

Add comment

Loading