Migrations and Seed Data in EF Core (New Updated Video in the Description)

Поделиться
HTML-код
  • Опубликовано: 21 авг 2024
  • ►► Master Web API development Best Practices: bit.ly/3TnqoFQ
    ►► Build great web apps in Blazor WebAssembly: bit.ly/437g87T
    ►► Support us on Patreon and get the source code: / codemaze
    UPDATED VIDEO IS HERE: • Entity Framework Core ...
    In this video, we are going to learn how to create Migrations, seed data, and make automatic migrations with Entity Framework Core. Also, we will show you how to remove and script migration files.
    LINKS MENTIONED IN THE VIDEO:
    To visit the whole article and download the source code visit:
    ►► code-maze.com/...
    To learn more about the EntityFramework Core, visit:
    ►► code-maze.com/...
    To visit our site:
    ►► code-maze.com/
    FOLLOW US ON SOCIAL MEDIA!
    ►► / marinko-spasojevic
    ►► / codemazeblog
    ►► / codemazeblog
    VIDEO SUMMARY:
    In this video, we are going to cover Migrations and Seed data features in Entity Framework Core.
    Migration is a way to keep the database schema in sync with the Entity Framework Core model by preserving data.
    The migration process has two steps: Creating migration and Applying migration. As we already said, our database schema must be aligned with the database model and every change in that model needs to be migrated to the database itself.
    We have prepared a project with the database model containing entity and context classes and applied different configuration options. Additionally, we have installed the required libraries and registered ApplicationContext as a service in the Startup class. All of these are required to support DI Entity Framework Core usage in our application.
    Entity Framework Core provides a method called Migrate to execute migration actions for us. So, with these preparations in place, all we have to do is create and execute migrations with a set of simple commands.

Комментарии • 45

  • @CodeMaze
    @CodeMaze  5 месяцев назад

    Thank you all for watching and for your support.
    ►► If you want to master Web API development using best practices, check out our Web API book: bit.ly/3x75ZMM
    ►► Also, to build great full-stack apps with Blazor, check out our course: bit.ly/3Pw3Y33

  • @PorkopekPeres
    @PorkopekPeres 4 года назад +3

    So useful. I didn't know I could create a stored procedure in migrations. Your videos always point out some features that are not so well known. Thank you!

    • @CodeMaze
      @CodeMaze  4 года назад

      Thank you Jose for the support. We appreciate that.

  • @prakashcs2435
    @prakashcs2435 4 года назад +1

    Simply superb dude..

    • @CodeMaze
      @CodeMaze  4 года назад +1

      Thank you very much Prakash. We are glad you enjoyed it.

  • @aldkdenedik2451
    @aldkdenedik2451 5 месяцев назад +1

    So useful.

    • @CodeMaze
      @CodeMaze  5 месяцев назад

      Glad to hear that! Thank you for watching.

  • @PorkopekPeres
    @PorkopekPeres 4 года назад +1

    So useful too the IHost extension to start migrations

    • @CodeMaze
      @CodeMaze  4 года назад

      Yeah it is. Especially when we want our readers just to start the app and have everything in the database prepared :D

  • @assylzhanshabkhatov5763
    @assylzhanshabkhatov5763 4 года назад +1

    Thank you so much. Powerful knowledge

    • @CodeMaze
      @CodeMaze  4 года назад

      You are very welcome. We are glad you like it.

  • @niroshanmanoharan4295
    @niroshanmanoharan4295 3 года назад

    Very informative video. Thanks a lot for the knowledge sharing.

    • @CodeMaze
      @CodeMaze  3 года назад +1

      You are very welcome. Thank you for watching the video.

  • @satishverma657
    @satishverma657 4 года назад

    Very useful, Thanks for sharing...

    • @CodeMaze
      @CodeMaze  4 года назад

      Thank you very much for watching.

  • @gleamofcolor
    @gleamofcolor 4 года назад +1

    very useful content :)

    • @CodeMaze
      @CodeMaze  4 года назад

      Glad you think so! Thanks a lot for the comment.

  • @akhilbandari629
    @akhilbandari629 3 года назад

    Hi
    Thank you for sharing the information
    It would be really great if you can do a few more videos on Migrations

    • @CodeMaze
      @CodeMaze  3 года назад

      Hi. Thank you for watching the video. What exactly do you have in mind?

    • @akhilbandari629
      @akhilbandari629 3 года назад

      @@CodeMaze Thank you for your response
      Recently I had an issue while changing the datatypes with the existing entity data
      I saw that most of the code blogs, doing some kind of custom migrations, but it was a little bit confusing it would be really great if you can share your thoughts or ideas on how to handle datatype changes using code first apporach

  • @Time21
    @Time21 4 года назад +2

    Microsoft has to fix namespace renaming after moving classes from one class library to another. Its a major drawback in Visual Studio i believe.

    • @CodeMaze
      @CodeMaze  4 года назад +1

      Well it would be nice for sure, but until that happens, we will have to do it manually.

  • @aslalsujath1341
    @aslalsujath1341 Год назад

    Hi Sir , Hope you are doing well ,
    My question is , am i need to create each table configuration, to add sample data , Like
    StudentConfiguration : IEntityTypeConfiguration
    SchoolConfiguration : IEntityTypeConfiguration so on .

    • @CodeMaze
      @CodeMaze  Год назад +1

      This is the best way to separate data seed for each entity. You don't have to create a separate class if you don't want to, you can add all the logic inside the Context class, but this is not something we would do.

    • @aslalsujath1341
      @aslalsujath1341 Год назад

      @@CodeMaze I appreciate the way of you explanation. More useful. Thanks a lot.
      you are using guid.newguid for id but my case i wanna use int for id. its show error can't duplicate. how can I pass int using this method.

  • @randalvc
    @randalvc 4 года назад

    It might be better if we start using the dotnet cli instead for the migrations.

    • @CodeMaze
      @CodeMaze  4 года назад

      You are always free to do that if you want to do things that way.

  • @TormodSteinsholt
    @TormodSteinsholt 2 года назад

    I didn't understand what the options.MigrationAssembly (re)solved. EFCoreApp was already the target and it was referencing the Entities assembly. I don't understand which clarification was necessary. I understand that EFCoreApp does not "match" Entities, but I didn't understand why it suddenly didn't "see" EFCoreApp.

    • @CodeMaze
      @CodeMaze  2 года назад

      Hello Tormod. Migration files are not in the same project as the ApplicationContext class. That said, without the options.MigrationsAssembly setup, EF Core will search for migration files inside the project where the Context class is. Since the Context class is now in the Entities project, this is where EF Core tries to find migration files, and why we get an error.

  • @mp-fg2zw
    @mp-fg2zw 4 года назад

    Hi Sir... Request you to provide a video on generating the model\table using Reflection in entityframework. Thank You.

    • @CodeMaze
      @CodeMaze  4 года назад

      Hi. We'll see about that. Sounds interesting.

    • @mp-fg2zw
      @mp-fg2zw 4 года назад

      @@CodeMaze Thank you Sir.

  • @llN3M3515ll
    @llN3M3515ll 3 года назад

    When using a many-to-many relationship with EF Core 5, can the intermediary table be automatically populated? Such as Class has many students, and students has many classes, ie a collection on both C# classes of the other, EFCore generated a StudentsClasses table with ID's of both Student and Class. Can that table be programmatically populated as well?

    • @CodeMaze
      @CodeMaze  3 года назад

      I just tried it with .NET 5 and if I create only StudentConfiguration class and try to seed Student data with Class attached for each object, EF Core complains that it requires a new StudentClass object... So maybe, I didn't research it more, you need to have a third class in the project and create your configuration there, as we did in this article: code-maze.com/efcore-relationships/#manytomany Just try adding the seed part in the third class.

  • @niltechit2189
    @niltechit2189 2 года назад

    How call Multiple Entity and Migration of Automatic >..

  • @testinnroad4379
    @testinnroad4379 3 года назад

    I have used the modeBuilder.Entity.totable("Student") along with properties.
    The database is created but tables cannot be seen in it.
    can i seed the database along with tables without adding migration manually

    • @CodeMaze
      @CodeMaze  3 года назад

      If I understood your question correctly, I see no problem in creating a single migration file dealing with both table creation and data seeding actions.

  • @gleamofcolor
    @gleamofcolor 4 года назад

    May I know, how to call SQL method inside the configuration class as it only allows to add seed data and entity configuration , only i can call the sql method inside of my migration file.

    • @CodeMaze
      @CodeMaze  4 года назад

      Configuration class inherits from the IEntityTypeBulder and it's meant to be used for the entity configuration with fluen api and data seeding. I am not sure what SQL methods are you refering to?

    • @gleamofcolor
      @gleamofcolor 4 года назад

      @@CodeMaze I am expecting SQL method similar to migration builder class. If I want to add seed data for country table, I can populate it via configuration class instead of populating it via migration class.