Usually, when I have an application that has an API and a Database managed with EF, I really don't like to have the Application DbContext and all the Migrations inside the API project. It's actually very easy to split it out, this is a quick post on how we can maintain Entity Framework Core DbContext in a separate project.
I have created a Solution WebApplication1 which has 2 projects, one is ASP.NET Core Web API and the other one just a .NET Core Class Library where I am maintaining Database specific things.
 |
Solution |
For WebApplication1.Data project, I have installed Microsoft.EntityFrameworkCore.SqlServer package and my WebApplication1.Data.csproj looks like this.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" />
</ItemGroup>
</Project>
And I have a very simple ApplicationDbContext.
using Microsoft.EntityFrameworkCore;
namespace WebApplication1.Data
{
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<Employee> Employees { get; set; }
}
}
Then inside my
WebApplication1.Api project, I have referenced
WebApplication1.Data project and installed
Microsoft.EntityFrameworkCore.Tools package there. This is very much required. This is my
WebApplication1.Api.csproj looks like.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WebApplication1.Data\WebApplication1.Data.csproj" />
</ItemGroup>
</Project>
I have the Connection String inside
appsettings.json and setup the
ApplicationDbContext inside
Startup.ConfigureServices method.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("ApplicationDbContext"));
});
}
So basically that's it. Using the
Package Manager Console in Visual Studio, I can run
Add-Migration command targetting
WebApplication.Data project.
 |
Package Manager Console |
Or alternatively using
dotnet ef global tool (you need to install the tool first). The parameters are self-explanatory.
dotnet ef migrations add Initial --project .\WebApplication1.Data\WebApplication1.Data.csproj --startup-project .\WebApplication1.Api\WebApplication1.Api.csproj
And it will add all the Migrations inside
WebApplication.Data project which is what I wanted.
 |
Migrations |
To apply the migrations, again using the
Package Manager Console in Visual Studio, I can run
Update-Database command targetting
WebApplication.Data. Or using the CLI,
dotnet ef database update --project .\WebApplication1.Data\WebApplication1.Data.csproj --startup-project .\WebApplication1.Api\WebApplication1.Api.csproj
That was easy.
Happy Coding.
Regards,
Jaliya