ASPCode.net logo
  • Home 
  • Blogs 
  • Tags 
Blog
  1. Home
  2. Articles
  3. C# - EF Core - Code FIrst Intro

C# - EF Core - Code FIrst Intro

Posted on October 31, 2024  (Last modified on May 26, 2025) • 3 min read • 476 words
Share via
ASPCode.net
Link copied to clipboard

Video is in Swedish

On this page
  • Code First with Entity Framework Core in C#: An Introduction
  • What is Code First?
  • Getting Started with EF Core
  • Configuring EF Core
  • Creating the Database
  • Inserting Data
  • Conclusion
  • Video
  • Sourcecode

Code First with Entity Framework Core in C#: An Introduction  

As developers, we’re always looking for ways to streamline our workflow and improve the efficiency of our code. One powerful tool that can help us achieve this is Entity Framework Core (EF Core), a popular Object-Relational Mapping (ORM) framework developed by Microsoft. In this article, we’ll explore how to use EF Core with Code First approach in C#.

What is Code First?  

Code First is an approach to database development where you write your code first and then generate the database schema from it. This approach allows you to focus on writing your business logic without worrying about the underlying database structure.

Getting Started with EF Core  

To get started with EF Core, you’ll need to install the NuGet package in your .NET Core project. You can do this by running the following command in the Package Manager Console:

Install-Package Microsoft.EntityFrameworkCore

Once installed, create a new C# class library project and add the necessary using statements:

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace CodeFirstEFCore
{
    public class MyDbContext : DbContext
    {
        public DbSet<MyEntity> MyEntities { get; set; }
    }

    public class MyEntity
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

In the above code, we’ve created a MyDbContext class that inherits from DbContext. We’ve also defined a MyEntities property of type DbSet<MyEntity>, which represents a collection of MyEntity objects.

Configuring EF Core  

To configure EF Core, you’ll need to create a database connection string and add it to your project. You can do this by adding the following code to your appsettings.json file:

{
    "ConnectionStrings": {
        "DefaultConnection": "Server=localhost;Database=MyDB;User ID=myuser;Password=mypassword;"
    }
}

Next, create a new instance of DbContextOptionsBuilder and pass it the connection string:

var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));

Creating the Database  

Now that we’ve configured EF Core, let’s create the database. We can do this by calling the Database.EnsureCreated() method on our MyDbContext instance:

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

Inserting Data  

To insert data into our database, we can use the Add method on our DbSet<T> property. For example:

using (var context = new MyDbContext(optionsBuilder.Options))
{
    var myEntity = new MyEntity { Name = "John Doe" };
    context.MyEntities.Add(myEntity);
    context.SaveChanges();
}

Conclusion  

In this article, we’ve explored how to use EF Core with the Code First approach in C#. We’ve created a MyDbContext class that inherits from DbContext, defined a DbSet<MyEntity> property, and configured EF Core using a database connection string. We’ve also created a database, inserted data into it, and saved our changes.

By following this tutorial, you should now have a good understanding of how to use EF Core with Code First in C#. This approach can help you streamline your workflow and improve the efficiency of your code. Happy coding!

Video  

Swedish

Sourcecode  

Sourcecode
 
 C# - Advanced - Class Library
React Del 5 State Context API 
On this page:
  • Code First with Entity Framework Core in C#: An Introduction
  • What is Code First?
  • Getting Started with EF Core
  • Configuring EF Core
  • Creating the Database
  • Inserting Data
  • Conclusion
  • Video
  • Sourcecode
Follow me

I code in Java, C#, Golang, SQL and more

     
© 2024 Systementor AB
ASPCode.net
Code copied to clipboard