Re-create table with Entity Framework 5 and nuget

ef-migrations entity-framework nuget

Question

I'm making a table with Code First.

I built the class, the mapping file, then used Nuget to run the add-migration and update-database commands.

Then, after changing the class, I foolishly removed the table.

The migration class file was removed.

I sent a command to add migration.

I receive the next error when I run the update-database command:

System.Data.SqlClient.SqlException (0x80131904): Cannot find the object "dbo.CorrectiveActionPlan" because it does not exist or you do not have permissions. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable1 migrationStatements) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable1 operations, Boolean downgrading, Boolean auto) at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable
1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() ClientConnectionId:a6e92a35-cc9e-4867-97a5-0a274081d853 Cannot find the object "dbo.CorrectiveActionPlan" because it does not exist or you do not have permissions.

How can I make EF regenerate the table on demand?

1
16
1/7/2014 6:49:44 PM

Accepted Answer

I got my response.

I removed the [dbo] row. My Migration was represented by [__MigrationHistory]

Next, I got rid of the new migration file.

I ran add-migration again.

updated the database again while being verbose.

33
11/15/2012 7:05:01 AM

Popular Answer

migrations using code initially has a few alternatives that I keep on hand, and they vary depending on why you need to destroy the tables or the entries. Here are my techniques:

  • You could Using SQL Management Server Studio, delete the Migration folder and the whole database. if you changed the models and the mappings are producing an error that prohibits you from updating the tables. You might wish to save a SQL script to re-populate your test data, or you might want to save your Configuration.cs file so that the data will be re-populated when you run the update database command.

    Here is an example of a stored procedure script for dropping table data only:

    USE [DatabaseName]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE PROCEDURE [dbo].[sp_DeleteAllYardPenaltyRecords]
    AS
    EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
    EXEC sp_MSForEachTable 'ALTER TABLE ? DISABLE TRIGGER ALL'
    EXEC sp_MSForEachTable 'DELETE FROM ?'
    EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
    EXEC sp_MSForEachTable 'ALTER TABLE ? ENABLE TRIGGER ALL'
    EXEC sp_MSFOREACHTABLE 'SELECT * FROM ?'
    
  • You can use the package management console command to simply drop the data:PM> Update-Database -TargetMigration $InitialDatabase Rerun "Add-Migration new example.cs" and delete the migration file that was created, i.e. "201502210525393 example with error.cs." This restores the database's original picture state.

  • You could also take your approach and remove the row from[dbo].[__MigrationHistory] run the migration file, such as "201502210525393 example with error.cs," again.add-migration and update-database



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow