EF Core Migrations Deep Dive, Applying Migration, SQL Scripts

Поділитися
Вставка
  • Опубліковано 29 вер 2024

КОМЕНТАРІ • 77

  • @MilanJovanovicTech
    @MilanJovanovicTech  3 місяці тому +1

    Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
    Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt

  • @s1kky
    @s1kky 3 місяці тому +11

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

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +1

      Maybe there's something to it 😁

    • @lashuti31
      @lashuti31 3 місяці тому +2

      Bro for real. Whenever im interested in some topic, Milan drops a video for it 😄😄

  • @PelFox
    @PelFox 3 місяці тому +1

    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.

  • @WantMore-mc8dx
    @WantMore-mc8dx 3 місяці тому +5

    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.

    • @Aweklin
      @Aweklin 3 місяці тому

      Exactly my approach too. And this has made me not to even worry about destructive commands.

    • @pazzuto
      @pazzuto 3 місяці тому

      I do this as well. In fact, I have a SQL project in the solution where the design happens and get a DACPAC that I can validate with the DBA. It's also deployed to CI/CD for all or none approach. I like this better than magic migrations.

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +2

      That's perfectly fine 👌 I've done it on some projects. But my SQL DDL skills aren't that sharp.

  • @wolecharlesjob8255
    @wolecharlesjob8255 3 місяці тому

    Great. Thanks Milan

  • @assylzhanshabkhatov5763
    @assylzhanshabkhatov5763 3 місяці тому +1

    How to revert some specific applied migration correctly? (Not the latest) Let’s say 10th of 20

  • @KyleStay
    @KyleStay 3 місяці тому +2

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

    • @b0ysb3
      @b0ysb3 3 місяці тому +2

      better reset it and scaffold from the latest conditions

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +1

      Recreate them, pretty much, where you only end up with one migration

  • @sinthonseng8981
    @sinthonseng8981 3 місяці тому

    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?

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      SQL? 😅

    • @krccmsitp2884
      @krccmsitp2884 2 місяці тому

      When you already have an existing database you can also use EF Core, but with the "database first" approach (also called reverse engineering). With it, you would generate source code (entity types) from the db objects, instead of creating db objects from your source. This way round, migrations as a code-first feature, are off-table.

  • @DuQuels93
    @DuQuels93 3 місяці тому

    I like to use liquibase instead of the migrations

  • @mohamedadelaboel-dahab8865
    @mohamedadelaboel-dahab8865 3 місяці тому

    second

  • @AbuBakrSadiqi-b7t
    @AbuBakrSadiqi-b7t 3 місяці тому +6

    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.

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      Thank you very much, I appreciate that! :) And I'm glad you find the content helpful, that's all I care about.

  • @curtmantle7486
    @curtmantle7486 3 місяці тому +2

    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.

  • @rafekemmis3097
    @rafekemmis3097 3 місяці тому +3

    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.

  • @chamikagoonetilaka4026
    @chamikagoonetilaka4026 3 місяці тому +2

    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.

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      This is an excellent approach 👌

    • @ValentinasKaminskas
      @ValentinasKaminskas Місяць тому

      Hello ​ @MilanJovanovicTech & @chamikagoonetilaka4026
      I have a question about this approach. Do you use a separate project specifically for managing database migrations, or is DbUp integrated directly within the main application?
      Additionally, how does DbUp handle database rollbacks if you need to revert the application version? For example, if your application is running in Kubernetes with three instances, each instance attempting to update the database could cause issues. I assume you manage database migrations with a separate service or project to avoid this.
      In a scenario where you roll back the application version (e.g., by deploying an older Docker image without making a new release), how would you manage the rollback of database changes using DbUp?
      Thank you!

  • @ZoneInOn
    @ZoneInOn 3 місяці тому +1

    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!

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +1

      It's a bit difficult discussing every concept from scratch in every video, so that's a valid critique. With these newer videos, I'm trying better to include context for pros/cons and various approaches you could take

  • @Bobo-nc5sb
    @Bobo-nc5sb 2 місяці тому

    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.

  • @cas818028
    @cas818028 3 місяці тому +1

    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

  • @GregoryGrewe
    @GregoryGrewe 3 місяці тому +2

    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?

    • @10Totti
      @10Totti 3 місяці тому

      Exact!

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +1

      I don't like having attributes on entities, but that's a personal preference. Agree on the IEntityTypeConfiguration.

  • @haroldpepete
    @haroldpepete 3 місяці тому +1

    great video, but i prefer use and external class to configure each entity, one file per entiy, imagine when you have more than 6 or 7 tables and all configuration is in the same place, that would be a nightmare, the class must have the decorator EntityTypeConfiguration. and you can check if there is any pending migration and run the service to run migrations, the app will not create a service in vain

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      That's fine, most people will end up doing it like that. The focus was more on the migrations here than EF config.,

  • @jarkal82
    @jarkal82 3 місяці тому +3

    Dziękujemy.

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      Zapraszamy! :) (hopefully translate didn't butchter it)

  • @ekekw930
    @ekekw930 3 місяці тому +1

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

    • @10Totti
      @10Totti 3 місяці тому

      No if you omit it will become property required.

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +1

      This is nullable reference types in C#. In general, it can still be null if there's no value.

  • @matinaminsabouri
    @matinaminsabouri 2 місяці тому

    Very Nice

  • @toyin5522
    @toyin5522 3 місяці тому

    Aint indexes by default unique?

  • @antonmartyniuk
    @antonmartyniuk 3 місяці тому

    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.

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +1

      It looks like you also take great care of generating "good" migration scripts, so it's easier to control issues

  • @clebersondot-net
    @clebersondot-net 3 місяці тому

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

  • @matejskerjanc7703
    @matejskerjanc7703 3 місяці тому

    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:)

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +1

      They don't go with one another. If you call EnsureCreated, Migrate will fail. You can generate the SQL scripts and run them.

    • @matejskerjanc7703
      @matejskerjanc7703 3 місяці тому

      @@MilanJovanovicTech so if I wanted new tables added I have to use scripts? Ooorrr continue working with ensure created that could also work, or just tear band aid off and have downtime in prod while proper steps are taken to have migrations setup and then restore backup, these were the options I see sadly

  • @10Totti
    @10Totti 3 місяці тому

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

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      Not sure what you mean? 🤔

    • @10Totti
      @10Totti 3 місяці тому

      @@MilanJovanovicTech You can do the same procedures directly from Visual Studio using a graphical interface

  • @baranacikgoz
    @baranacikgoz 3 місяці тому

    Using sql query approach to validate if scripts are correct? I am 100% sure that you can make mistakes in sql but an open sourced, Microsoft leading EFCore won't create a false script. There are no value in using this approach unless the company you work ensures high security like a bank and the connection string of the app does not have alter permissions etc.

    • @vr77323
      @vr77323 3 місяці тому

      Can you elaborate on this? I don't quite get it. Do you mean that generating a script from your migrations to be able to validate it adds no value and that EFCore will make a better migration than me?
      Because if that is what you are suggesting then that's not exactly the case. Just because microsoft is behind it and it's open source does not mean it will always get things correct. Simplest example 1: What he showed with older ef versions where a column rename will be executed as a drop-create instead of a rename. That's a very destructive command as if you apply this on a production db, it will delete all data in the column
      Simple example 2 (This was more on me for not configuring ef correctly at the time, but that is also a very plausable case): I had created some entities with FK between them, but ef couldnt figure out which property points to which one exactly, so what it did instead was create "shadow" FKs called Id1, Id2, etc... this is also not a very good production solution and I managed to catch and solve it by looking through the migration/script generated by EF.
      Remember, efcore, no matter who stands behind it, works on configurations and assumptions. Whatever it generates is based on your configuration and it's own assumptions. You can never be 100% sure it won't create something bad just because a big tech company is behind it. Everyone makes mistakes

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому

      EF scripts can sometimes do destructive operation, which you should absolutely double check before running it on your database.

    • @johnsuckher3037
      @johnsuckher3037 3 місяці тому

      reorganize structure and suddenly it's just dropping operations with no data movement

  • @2PlayGaming
    @2PlayGaming 3 місяці тому +2

    No, thank you, too many type to coding and thinking, I'll use ef core power tools to generate a context in minutes...

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 місяці тому +2

      But that only works with an existing database?

    • @2PlayGaming
      @2PlayGaming 3 місяці тому +1

      @@MilanJovanovicTech exactly. Only if your database is created.

    • @10Totti
      @10Totti 3 місяці тому

      @@MilanJovanovicTech Yes.

    • @blackpaw29
      @blackpaw29 3 місяці тому +2

      @@MilanJovanovicTech True - but when developing now product we tend to do database first and scaffold from the db. Once its out there, then we start using migrations, but still scaffold from the development db

    • @flygonfiasco9751
      @flygonfiasco9751 3 місяці тому +1

      @@MilanJovanovicTechI think they’re suggesting they prefer to create a database then scaffold the context. I think this is a nice approach for more sql-focused developers