Generate missing migrations so I can update an old database structure - entity-framework-migrations

I am using EF Code first with Migrations. I need to update an old database
however I have lost some of the migration code.
Is there a way to generate the missing migrations?

Step 1: Back up ( obviously )
Step 2: Delete __MigrationHistory
Step 3: Create a temporary project.
Use the Entity Data Model Wizard to create the Code-First Model.
Step 4: Enable migrations in the new project ( using Enable-Migrations) at the Package Manager Console.
Create an initial migration ( using add-migration one). Comment out the Up and Down methods of the created migration.
Run update-database.
Step 5: Replace all the files in the Migrations folder of the original project with the files in the temporary project. Editing DbContext and namespace names as required.
Step 6 In the original project in package manager run Add-Migration two
then run update-database

Related

Not able to add migration and updating database in dotnet core ef

I am using generic DesignTimeDbContextFactory in Dotnet core 2.2.1 version for database.
I am trying to add migration for which, a class created under the migration folder but didn't reflect the changes in UP and Down and also in ContextModelSnapShot file.
if I try with dropping ContextModelSnapShot, then changing reflected. but the issue here is it will build a table but also dropping the table in DOWN.
Can anyone help me to come out through this issue?
In my case, the problem is the previous changes file is exists which already did by the previous developer who is working on it. I just temporary remove that file from the migration folder for that particular context as i have multiple contexts in my app and try to do migration will work for me. it was conflicting with the previous files.

Tables from model are not in server explorer tables

I am creating my first ASP.NET MVC application and I have a problem with Entity Framework.
I created 4 tables, added them to DbContext object and then I saved my project and closed it. All of these tables are in server explorer -> tables.
Then I created next 5 tables and added them to DbContext object. Unfortunately I cannot find them in server explorer -> tables. I tried to refresh, delete tables and add one more time but it does not work.
Do you know how to solve this problem (I used code first method) ?
Yes, you are missing migrations. Migrations are used when you need to update your database schema. If you add those tables (models) to your context it wont create them in your database until you add the migration.
With migration, it will automatically update the database schema, when your model changes without losing any existing data or other database objects.
Source
1. step
Run Visual Studio -> Tools –> Library Package Manager –> Package Manager Console
2. step
Run the Enable-Migrations command in Package Manager Console - Enables migrations on your project that contains your context.
3. step
Add-Migration AddedFiveNewTables - Prepares the script that contains all the new changes to your database.
4. step
Update-Database - Runs the previously added migration and updates your database.

Second attempt to generate script sql script did not work

I am developing an application in ASP.NET MVC 5 and Entity Framework with a code-first approach.
Firstly, I had one class in my data context file and everything worked. enable-migrations, add-migration and update-database were working fine but second time, I added more classes and tried to execute these statements:
enable-migrations -ContextTypeName IdentityDb -MigrationsDirectory DAL\IdentityMigrations
enable-migrations -ContextTypeName SMSContext -MigrationsDirectory DAL\SMSMigrations
add-migration -ConfigurationTypeName SMSApp.DAL.IdentityMigrations.Configuration "InitialCreate"
I get an exception:
A previous migration called 'InitialCreate' was already applied to the target database. If you meant to re-scaffold 'InitialCreate', revert it by running 'Update-Database -TargetMigration $InitialDatabase', then delete '201409261933262_InitialCreate1.cs' and run 'Add-Migration InitialCreate' again.
In Solution Explorer click icon Show All Files. Than go to App_Data, right click on mdf file and open.
In Server Explorer click on connections and right click on your connection then close it, then right click again then delete it, then delete the mdf file from the Solution explorer(this will move it to the trash bin, it is not lost yet if you decide to get it back). Then in power shell package console update-database.
NOTICE: This will totally rebuild your database inserting records from Configuration/Migrations.cs Seed method, you will lose other data. Also find AutomaticMigrations in the code and set it to true.

Entity Framework 5 Code First Migrations: Get database script

I've a project following the course of John Papa about SPA.
So, my architecture is as follows:
**proj.Data.Data**
/Configuration
Mappings of the entities with Fluent
ProjContext.cs
ProjInitializer.cs
EFRepository.cs
/Migrations folder
**proj.Data.Model**
Pocos (Entity Framework 5 Code First)
No reference to Entity Framework
**proj.Data.Contracts**
Interface for IRepository and IProjContext
**proj.UI.Web**
MVC 4/WebApi project
I had an existing database and I ran reverse engineering of Entity Framework to get the entities and separated the context from the entities (in two different projects as shown above)
Then I ran (selecting proj.Data.Data from the PackageManager Console drop down):
Enable-Migrations -force -StartupProjectName "Proj.Data.Data"
add-migration InitialSchema
Update-Database
Update-Database -Script -SourceMigration $InitialDatabase
I only see in the folder Migrations in the proj.Data.Data project a file named 201211091944388_InitialSchema with the up and down methods empty. No MigrationHistory table created.
What do I need to run to get the sql script to generate the database from scratch with all the tables I have got from the entities?
Thanks! Guillermo.
NOTE: I have .Data projects in a solution folder named Data and Web project in a solution folder named UI, Should I take any extra step because of that?
I don't know why it didn't work with the reverse engineering, but I made the model again following all the conventions and it worked.
BTW, if we run the enable-migrations command on an existing database it will not create __MigrationHistory table nor the Initial script. This is as design to avoid changes in the existing database.
Thanks anyways.

Entity Framework Code First error with testing project

I am using Entity Framework's code first approach and updating my database with changes to my DB model using the following command.
Update-Database -Script
This was all working very well until recently I added some connection strings in the App.Config for my testing application. Now when I run the update-database -script command I get the following error. Does anyone know why this is happening or how to suppress it? Thanks in advance.
Update-Database : The project 'MyProject.Tests' does not contain any migration contexts.
Check on you Packet Manager console that the Source Project is the same where the Migrations Folder exists.

Resources