Entity Framework Core (EF Core) makes handling database migrations easier, but running them in a release pipeline can get tricky. Normally, you’d need the .NET SDK and EF CLI tools, which adds extra setup steps. That’s where EFBundles come in! They package your migrations into a single executable, making deployments smooth and hassle-free.
Why Use EFBundles?
No need to install extra tools in the pipeline.
Ensures migrations run the same way everywhere.
Simplifies deployment by bundling everything into one file.
No need to worry about writing SQL scripts.
EFBundles are a feature introduced to streamline the process of applying Entity Framework migrations in automated environments, such as CI/CD pipelines. Typically, running EF migrations requires the .NET SDK and EF CLI tools, which adds complexity to setting up and maintaining build pipelines. EFBundles solve this problem by packaging migrations into a single, self-contained executable, eliminating the need for these external dependencies.
By bundling all the migration logic into one file, EFBundles ensure consistent behavior across different environments, whether you're running the migrations on your local machine, a test server, or in a cloud-based CI/CD pipeline. This simplifies the deployment process and ensures that your database schema is always in sync with your application code, with minimal setup and configuration.
Setting Up efbundles
1. Install the dotnet-ef
tool
First, make sure you have EF Core tools installed:
dotnet tool install --global dotnet-ef
One thing to mention here, In the CD pipeline you need to be concerned about your dotnet-ef version.
dotnet tool install --global dotnet-ef --version 8.0.0
2. Create the efbundle
Now, let’s generate a self-contained executable for migrations:
dotnet ef migrations bundle --self-contained -r win-x64 --force \
--project <Path-To-The-Project-csproj-file> \
--startup-project <Path-To-The-Startup-csproj-file> \
--context <DbContext-Name> \
--output <Path-To-Output-Bundle>
Tip: Use
linux-x64
instead ofwin-x64
if your pipeline runs on Linux.
This creates an efbundle.exe
(Windows) or efbundle
(Linux/macOS) that can apply migrations.
3. Store the Bundle (Optional)
If you want consistency across builds, you can commit the bundle to your repository.
Running EFBundles in a Release Pipeline
After creating the EFBundles, you can run them in a terminal or command prompt:
# Windows
./efbundle.exe --connection "<Your_Connection_String>"
# Linux/macOS
./efbundle --connection "<Your_Connection_String>"
Example: Azure DevOps Workflow
Here’s how you can apply migrations in an Azure DevOps CI/CD pipeline:
Create the artifacts which means the efbundles as above mentions and publish them. Next, just run them in the Release pipeline. And.. it is easy as it looks. ;)
variables:
YourConnectionString: '<Your_Connection_String>'
steps:
- script: |
$(System.DefaultWorkingDirectory)/<Your_Artifacts_Publishing_Directory>/efbundles/<Your_efbundle>--connection $(YourConnectionString)
displayName: 'Run Your efbundle.'
This ensures your database schema is always up to date during deployments.
Wrapping Up
EFBundles are a great way to simplify EF Core migrations in a CI/CD pipeline. By bundling everything into a single executable, you avoid dependency issues and make deployments much smoother. Try it out and streamline your database migrations today!