C# - EF Core - Code FIrst Intro
Posted on October 31, 2024 (Last modified on May 26, 2025) • 3 min read • 476 wordsVideo is in Swedish
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#.
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.
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.
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"));
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();
}
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();
}
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!
Swedish