Reputation: 9391
In EF, if I add a new entity which represents an existing table in the database I get an error stating I need to run a migration.
Is there anyway to add an existing table to my context without needing to create a new migration?
Seems pointless to have an empty migration like this...
public override void Up()
{
}
public override void Down()
{
}
I'm guessing every time I add entites it compares it to a serialised version in __MigrationHistory's model column.
Upvotes: 0
Views: 2224
Reputation: 5314
What we've done in this scenario is re-use the latest migration. You can rescaffold a migration by using the -force
switch and specifying the target migration. Make a copy of the current migration first to compare it after the force: you're essentially telling EF that it's OK to overwrite that file with whatever changes are pending. Here's the process:
update-database -target:"SecondMostCurrentMigration"
add-migration MostCurrentMigrationName -force
review the file against the copy you made to make sure nothing changed
Update the database to the most current migration, the existing file that you just 'modified'.
update-database
Upvotes: 0
Reputation: 3217
You have two choices in general:
Logs
table and write data using standard SQL provider. If you will add this table later to you DbContext
, migration will be created.Another possible solution is have two DbContexts
. In the first case you can use code first with migrations and the second could be DbContext used with reverse engineering way.
If you dont want to create migration whenever some change in your schema occurs, you can consider MigrateDatabaseToLatestVersion
initialiser.
Upvotes: 1