EF Core Migrations Deep Dive, Applying Migration, SQL Scripts

preview_player
Показать описание

Managing database schemas as your applications grow can quickly become a headache. Manual changes are error-prone and time-consuming, which can easily lead to inconsistencies between development and production environments. I've seen these issues firsthand on countless projects, and they're not pretty. How can we do better?

Enter Entity Framework (EF) Migrations, a powerful tool that lets you version your database schemas.

Imagine this: Instead of writing SQL scripts, you define your changes in code. Need to add a column? Rename a table? No problem - EF Migrations has you covered. It tracks every modification to the data model. You can review, test, and apply changes confidently, even across different environments.

EF Core Migrations: A Detailed Guide

Migration tooling:

Join my weekly .NET newsletter:

Read my Blog here:

Chapters
0:00 What are EF Core migrations?
2:13 Configuring an EF Entity
5:18 Creating a database migration
6:27 Creating another database migration
7:55 Applying EF Core database migrations
10:01 Generating SQL Scripts for EF migrations
12:11 Writing custom SQL in EF migrations
13:00 Additional database versioning tools
14:15 EF Core migrations best practices
Рекомендации по теме
Комментарии
Автор

I learned so much concepts from you. Following you from some months, sometimes I go to your channel and watch complete play lists. Thank you for doing so much for the community.

AbuBakrSadiqi-bt
Автор

I swear it feels like u have access to my company's repos, it's like you read my mind with these videos😂

skky
Автор

I use EFCore migrations with DbUp. One of the key advantages of this approach is you have the full control over the SQL scripts that are getting executed on the database, while enjoying the EFCore's ability to automatically generate SQL for you. Sometimes, you will need to add custom SQL scripts in-between the generated scripts to populate seed data and to do data migrations.

chamikagoonetilaka
Автор

The approach with generating SQL scripts from migrations is great!
In one of my company's products we have a lot of microservices and each microservice upgrades itself executing fluent migrations at a application startup. We're using fluent migrator because our product runs on different SQL databases depending on our clients. And this approach simplifies a lot and is perfectly fine. I can't remember a single time when we had to downgrade the database in production after a bad update.

antonmartyniuk
Автор

Creating and adding column(s) in a specific order would be a great point to be mentioned!!

waelsalama
Автор

Hey Milan, great video.

Sometimes my gripe with some of your videos is not showcasing the full extent of how things work, with practical examples (understandably because of time constraints IRL and for video length).
This video isn't one of those, you show many ways of doing it, how you do it, concerns to be aware of, actually implement it e.t.c - great video!

ZoneInOn
Автор

I love working with Migrations! Thank you for more videos like this, Milan!

clebersondot-net
Автор

I remember just cracking open red gate back in the day. Asking it to do a db diff and generate the script for me. Ran that in production without worry, hassle, or headache

cas
Автор

Tip #5 - we set the snapshot file to be binary in our git config so that they don’t automatically merge in the case where a pr created a migration without the latest migration snapshot. It’s not a magic bullet but it can at least throw a merge conflict on the pr and indicate that something is wrong.

rafekemmis
Автор

The hardest part is managing multiple PRs with migrations. When one completes the next ideally needs to Pull and rerun migrations and it can be a pain to manage code reviews.

curtmantle
Автор

My approach is to handle all DB stuff within SQL and not use migrations at all. Generated SQL-changes might be ok if it's smaller DB and you don't have to consider optimizations.

WantMore-mcdx
Автор

At 2:42 Description is of type 'string?' but isnt a string always nullable?

ekekw
Автор

When you have hundreds of migrations, are there best practices for consolidating them?

KyleStay
Автор

I generate a sql script during build and apply it in my CI pipeline. That way I can keep my app user limited to write and read.

PelFox
Автор

Typically, I use attributes in the Model, instead of the Fluent design, and use the Fluent design for more complicated implementation details. I believe this gives the developer one location to refer to when building out the application logic. Also, I use Apply Configuration and create Entity Builders, to minimize the code in the DbContext. Is there a reason that you did not reference SSMS as a DB Tool? Or how to execute SQL scripts directly from Visual Studio? Maybe you cover those in other videos?

GregoryGrewe
Автор

Nice video as usual. I have a question though, consider someone used ensure created command and didn't use migrations, how would you go around creating continuing with migrations from that point on without losing data, I know a bit out of scope, just something thats waiting for me soon, I see few options but all look bad:)

matejskerjanc
Автор

I am currently involved in a project that uses an existing SQL Server database, which includes over 300 views and over 500 stored procedures. I am planning to integrate EF Core into this project. Some of these procedures use user-defined tables as parameters and contain over 3000 lines of code.
Is this approach feasible, or is there an alternative solution for this scenario?

sinthonseng
Автор

Just had a bad Merge conflict with EF Core migrations and I am looking for a better way to manage these when more than 1 person is making changes at basically the same time.

michaelakin
Автор

I understand that the code-first approach of EF Core is very convenient for developing the application's database. However, I often encounter errors when applying migrations, especially when changing table keys.

Bobo-ncsb
Автор

Excellent video, a note that few say, it is possible to do all this also from the Visual Studio UI.

Totti