Delete a specific EntityFramework Migration - c#

Is there a way to delete an automatic migration in the migration history of a project using Entity Framework Migrations? This is a code-based EF project that's has a mix of explicit and automatic updates. There is an automatic update that deletes a table of content and isn't necessary. I'd like to be able to just remove it.
For some reason, the database it's running against doesn't have a __MigrationHistory table. (At least I don't see it in the list of tables for the databases or the list of System Tables in the database. Not sure if you have to enable viewing those somehow or not inside SSMS).
Please offer any advice you have. I'm open to whatever solution as long as I can maintain all that application's data.

I hope it is not too late.
The __MigrationHistory table is made in System Tables section on Target Database(Same as base mode Database)

Related

How do I handle database-first when the database doesn't have any links (and I can't add any)?

I have been asked to write a web site that will use an existing SQL Server database. The database was designed to work with another application, and so I can't make any potentially breaking changes to it.
Unfortunately, the database does not contain a single relational link, each table is standalone.
I know you can use EF commands to scaffold a database and create entity classes, but I would like the code to know the relationships that should exist between the tables.
I thought about scaffolding the database, then modifying the created classes to include the links, but I'm not sure if that would allow EF to load related entities. Also, as I will need to add some new tables to the database, I'm worried that EF will try and create those links when I do the migration.
Is there any way to do this?

Entity Framework Core Database First

My team has inherited a database application that contains hundreds of tables. The application uses Entity Framework and takes a database first approach for development. Our current process is to pull a table or two at a time into the edmx using the Update Model From Database... tool.
We are considering making a new API with .Net Core, but as far as I can tell from the research I have done, there is no equivalent process in the Entity Framework Core tools. The closest thing I can find is to reverse engineer the entire database with Scaffold-DbContext, and then use migrations for all future database changes. We can't scaffold the entire database, because some of the tables have errors, and fixing all those errors is not a viable option for us right now.
I have found that I can supply a list of tables that I want scaffolded with the initial Scaffold-DbContext call, but I'm not sure if migrations can be used in a similar way to the Update Model From Database... tool. Can I use migrations to add tables that already exist in our database? If not, what other options should I be looking at?

Generating MigrationHistory table in reverse engineered code first

I have reverse engineered on existing database for code first. Next i Enabled-Migrating for the context (Code based migration). When i create an initial add-migration it works fine and would apply on an empty database.
But my requirement is that i need to use the same database i used for creating the models because of the data it has.
Now the conundrum is how do i implement the code based migrations. My database does not have a migration history table. So, when i run Update-database, it tries to create the existing tables and fails.
How can i capture the current state in the migration history or instruct EF to create the migration history with the current schema as the starting point.
Do i need to turn on the automatic migration for initial setup. Please suggest.
Note: I am using EF 6.
You need to establish a baseline migration of the existing items. So the first migration you create should be:
add-migration Initial -IgnoreChanges
The ignore changes tells EF to just save a snapshot of the current database. Now any subsequent migrations will not include the existing tables (just the changes). This would allow you to continue updating your existing database since EF will see the record in __MigrationHistory or deploy to a new, empty database.
See the under the hood section here.

Database First - trying to recreate my entities that exist

I'm trying to connect my app to an existing Application.
So i created the Entities with the Entity Framework Database First.
It create all my entities and my Context that's fine.
But when i try to run it tell me that my migrations are not up-to-date so i tried to add-migration test to see what was missing...
It's creating all the table i asked it from DataBase First...
I cant recreate those tables i just want to be able to connect to those tables...
I absolutly need to connect to those tables, i cannot create a new one and i cannot clone the database.
How can i achieve this... i didnt find any resources on the subject that goes from the start to the end of the process.
Some help would be awesome!
The Problem: You can't reset migrations with existing tables in the database as EF wants to create the tables from scratch.
What to do:
Delete existing migrations from Migrations_History table.
Delete existing migrations from the Migrations Folder.
Run add-migration Reset. This will create a migration in your Migration folder that includes creating the tables (but it will not run it so it will not error out.)
You now need to create the initial row in the MigrationHistory table so EF has a snapshot of the current state. EF will do this if you apply a migration. However, you can't apply the migration that you just made as the tables already exist in your database. So go into the Migration and comment out all the code inside the "Up" method.
Now run update-database. It will apply the Migration (while not actually changing the database) and create a snapshot row in MigrationHistory.
You have now reset your migrations and may continue with normal migrations.

Extra table in database Entity Framework Code First With Database Migrations

Is it okay to have a table in the database that is not one of the entities when using code first with database migrations? Or will this interfere with the migrations? I want to put in a table to track some miscellaneous information.
We do this with no issues. We do add the table through the migration though so we we can ensure all developers have the same schema. If you want to skip this and go via SQL Management studio then you should have no issued either.
Sql(#"CREATE TABLE.....")

Categories

Resources