![]() It provides a simple and quick approach for developers to write queries or commands in C# language, while the EF Core takes up the task of translating it into equivalent SQL syntax and execute on the connected database. Then execute the following command.Entity Framework Core is one of the most popular Object-Relational Mappers (ORMs) in the market, particularly in the dotnet space. Suppose you want to roll back the database schema to any of the previous states, then you can execute the update-database command with the –TargetMigration parameter to the point which you want to roll back to.įor example, suppose there are many migrations applied to the above SchoolDB database but you want to roll back to the first migration. Now, whenever you change the domain classes, execute Add-Migration with the name parameter to create a new migration file and then execute the Update-Database command to apply the changes to the database schema. To see the examples, type: "get-help Update-Database -examples".įor more information, type: "get-help Update-Database -detailed".įor technical information, type: "get-help Update-Database -full".Īt this point, the database will be created or updated. Updates the database to the current model by applying pending migrations. ConnectionString -ConnectionProviderName PM> get-help update-databaseĪpplies any pending migrations to the database. Use the –verbose option to view the SQL statements being applied to the target database.Įxecute the get-help update-database or get-help update-database -detailed command in PMC to know more about the command. To see the examples, type: "get-help Add-Migration -examples".įor more information, type: "get-help Add-Migration -detailed".įor technical information, type: "get-help Add-Migration -full".Īfter creating a migration file using the add-migration command, you have to update the database.Įxecute the Update-Database command to create or modify a database schema. Scaffolds a new migration script and adds it to the project. Scaffolds a migration script for any pending model changes.Īdd-Migration To know more about add-migration command parameters, execute get-help add-migration or get-help add-migration -detailed commands in PMC, as shown below. This is the advantage over automated migration. You may also write your own custom code for additional configurations. The above command will create a _SchoolDB-v1.cs file with the Up() and Down() methods, as shown below.Īs you can see, the Up() method contains code for creating database objects and the Down() method contains code for dropping or deleting database objects. Now, you have to create a migration class using the Add-Migration command with the name of your migration class, as shown below. Protected override void OnModelCreating( DbModelBuilder modelBuilder) ![]() Public SchoolDBContext(): base( "SchoolDB")ĭatabase.SetInitializer( new MigrateDatabaseToLatestVersion()) Now, you need to set the database initializer MigrateDatabaseToLatestVersion in your context class, as shown below. The Enable-Migrations command will create the Configuration class derived from DbMigrationsConfiguration with AutomaticMigrationsEnabled = false. Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations command (make sure that the default project is the project where your context class is). To use code-based migrations, first execute the enable-migrations command in the Package Manager Console.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |