2012-11-15 9 views
16

Sto utilizzando Code First per creare una tabella.Ricrea tabella con Entity Framework 5 e nuget

ho creato la classe, il file di mapping e rilasciato il comando add-migrazione NuGet e poi il comando update-banca dati

Poi ho cambiato la classe e come un idiota cancellato il tavolo.

ho cancellato il file di classe di migrazione

Ho emesso un comando add-migrazione

Quando ho lanciare il comando update-banca dati ottengo il seguente errore:

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, Action 1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 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(TaskCompletionSource 1 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(IEnumerable 1 migrationStatements) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable 1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable 1 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(IEnumerable 1 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.

Come forzo EF per ricreare il tavolo?

risposta

31

Ho trovato la mia risposta.

ho cancellato la riga in [dbo]. [__ MigrationHistory] che corrispondeva alla mia migrazione

Allora ho cancellato il nuovo file di migrazione

I ri-ran aggiuntivo migrazione

e poi ri -ran update-database -verbose

+0

Mi hai salvato tempo serio! – Pinch

+0

Anche tu mi hai salvato un po 'di tempo!:)) – Xenon

+1

Sei il re della giungla! –

-1

Si potrebbe semplicemente eliminare il database, quindi dalla console del gestore pacchetti eseguire il comando 'update-database', tutto verrà ricreato includendo eventuali aggiornamenti effettuati.

+0

A meno che non ci fossero dati necessari. –

+0

@Eric: Vero, ma stavo offrendo una risposta diretta alla sua domanda (Come faccio a forzare EF a ricreare il tavolo?) - la parola chiave è "ricreare" –

0

Ci sono alcune opzioni che tengo nel mio arsenale per le migrazioni di codice e dipendono dal motivo per cui è necessario eliminare tabelle o eliminare i record. Ecco i miei metodi:

  • Se si è modificato i modelli e le mappature stanno causando un errore che impedisce di non essere in grado di aggiornare le tabelle si potrebbe Eliminare l'intero database utilizzando SQL Server Management Studio & Eliminare il Cartella di migrazione È possibile che si desideri salvare uno script per popolare di nuovo i dati di test utilizzando uno script sql o salvare il file Configuration.cs e quando si esegue il comando del database di aggiornamento, i dati verranno riseminati.

    Ecco un esempio di sceneggiatura per una stored procedure per cadere solo i dati della tabella:

    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 ?' 
    
  • Se si vuole solo far cadere i dati è possibile utilizzare il comando console di gestione dei pacchetti: PM> Update-Database -TargetMigration $InitialDatabase & eliminare la migrazione file creato ie: '201502210525393_example_with_error.cs' e rieseguire di nuovo 'Add-Migration new_example.cs'. Questo pone il database al suo snapshot iniziale

  • Oppure si potrebbe utilizzare il metodo: eliminare la riga in [dbo].[__MigrationHistory] & migrazione dei file vale a dire: '' 201502210525393_example_with_error.cs poi ri-run add-migration e update-database