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.
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
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!
Thank you Jose for the support. We appreciate that.
Simply superb dude..
Thank you very much Prakash. We are glad you enjoyed it.
So useful.
Glad to hear that! Thank you for watching.
So useful too the IHost extension to start migrations
Yeah it is. Especially when we want our readers just to start the app and have everything in the database prepared :D
Thank you so much. Powerful knowledge
You are very welcome. We are glad you like it.
Very informative video. Thanks a lot for the knowledge sharing.
You are very welcome. Thank you for watching the video.
Very useful, Thanks for sharing...
Thank you very much for watching.
very useful content :)
Glad you think so! Thanks a lot for the comment.
Hi
Thank you for sharing the information
It would be really great if you can do a few more videos on Migrations
Hi. Thank you for watching the video. What exactly do you have in mind?
@@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
Microsoft has to fix namespace renaming after moving classes from one class library to another. Its a major drawback in Visual Studio i believe.
Well it would be nice for sure, but until that happens, we will have to do it manually.
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 .
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.
@@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.
It might be better if we start using the dotnet cli instead for the migrations.
You are always free to do that if you want to do things that way.
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.
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.
Hi Sir... Request you to provide a video on generating the model\table using Reflection in entityframework. Thank You.
Hi. We'll see about that. Sounds interesting.
@@CodeMaze Thank you Sir.
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?
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.
How call Multiple Entity and Migration of Automatic >..
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
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.
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.
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?
@@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.