net-core
  1. net-core-code-first-and-database-first

Code-First and Database-First with ASP.NET Core & Entity Framework Core

Entity Framework Core (EF Core) is a popular Object-Relational Mapping (ORM) framework that allows you to interact with a database from a .NET application. There are two ways to work with EF Core: code-first and database-first. In this page, we will discuss how to use both approaches in an ASP.NET Core application.

Code-First

With the code-first approach, you start by defining the shape of your data model using C# classes, and EF Core uses that model to generate your database schema.

Syntax

Here's how to define a simple data model using the code-first approach:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public Blog Blog { get; set; }
}

Once you've defined your data model, you can use the EF Core DbContext class to define a database context and set up your data model for database creation.

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Integrated Security=True");
    }
}

Example

Here's how to create a new blog and add it to the database using the code-first approach:

var blog = new Blog { Url = "https://example.com" };

using (var context = new BloggingContext())
{
    context.Blogs.Add(blog);
    context.SaveChanges();
}

Output

The code-first approach generates the database schema based on your data model. When you save a new entity to the database, EF Core automatically creates the appropriate tables and relationships.

Database-First

With the database-first approach, you start by defining your database schema using tools like SQL Server Management Studio, and then use EF Core to generate the corresponding C# classes and DbContext.

Syntax

Here's how to generate C# classes from an existing database using the Scaffold-DbContext command in the .NET CLI:

dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -o Models

This command generates a DbContext and POCO classes based on your existing database schema.

Example

Here's an example of how to query data from the database using the database-first approach:

using (var context = new BloggingContext())
{
    var blogs = context.Blogs.Include(b => b.Posts);

    foreach (var blog in blogs)
    {
        Console.WriteLine($"Blog: {blog.Url}");

        foreach (var post in blog.Posts)
        {
            Console.WriteLine($"\tTitle: {post.Title}");
            Console.WriteLine($"\tContent: {post.Content}");
        }
    }
}

Output

The database-first approach generates C# classes based on your existing database schema. You can then use these classes to query data from the database and manipulate it in your application.

Explanation

Code-first and database-first are two approaches to working with EF Core in an ASP.NET Core application. Code-first allows you to define your data model using C# classes and have EF Core generate the database schema for you. Database-first allows you to start with an existing database schema and generate the corresponding C# classes and DbContext.

Use

Code-first is useful when you're starting a new project from scratch and want to define your data model using C# classes. Database-first is useful when you're working with an existing database schema and want to generate corresponding C# classes and DbContext.

Important Points

  • With code-first, EF Core generates the database schema based on your C# classes.
  • With database-first, EF Core generates C# classes based on your existing database schema.
  • Both approaches have their own strengths and weaknesses and can be used depending on your situation.

Summary

In this page, we discussed how to use both code-first and database-first approaches to work with EF Core in an ASP.NET Core application. We covered the syntax, example, output, explanation, use, important points, and summary of both approaches. By understanding and using both approaches, you can choose the one that best fits your needs and work more efficiently with EF Core in your ASP.NET Core application.

Published on: