Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-create table with Entity Framework 5 and nuget

I am using Code First to create a table.

I created the class, the mapping file and issued the add-migration command in nuget and then the update-database command

I then changed the class and like an idiot deleted the table.

I deleted the migration class file

I issued a add-migration command

When I issue the update-database command I get the following error:

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 do I force EF to recreate the table?

like image 288
James Helms Avatar asked Nov 15 '12 06:11

James Helms


People also ask

Which NuGet package is required for SQL Server in Entity Framework Core?

See the version list below for details. Requires NuGet 3.6 or higher. Microsoft SQL Server database provider for Entity Framework Core.

How to get started with Entity Framework 6?

Tutorial: Get Started with Entity Framework 6 Code First using MVC 5 1 Prerequisites 2 Create an MVC web app 3 Set up the site style 4 Install Entity Framework 6 5 Create the data model 6 Create the database context 7 Initialize DB with test data 8 Create controller and views 9 View the database 10 Conventions More items...

How to use Entity Framework with core MVC?

Introduction. This article is about Entity Framework with .Net Core MVC, Database-First approach. Step 1: Create an ASP.NET Core MVC application. Step 2: Reverse engineer Entity model from database (database first aproach for entity) Step 3: Scaffold Controller with View using Entity Framework. Step 4: Run and Test app.

How do I initialize a database in Entity Framework?

Initialize DB with test data. Entity Framework can automatically create (or drop and re-create) a database for you when the application runs. You can specify that this should be done every time your application runs or only when the model is out of sync with the existing database.

How do I add a new entity to the database?

The Seed method takes the database context object as an input parameter, and the code in the method uses that object to add new entities to the database. For each entity type, the code creates a collection of new entities, adds them to the appropriate DbSet property, and then saves the changes to the database.


1 Answers

I found my answer.

I deleted the row in [dbo].[__MigrationHistory] that corresponded to my Migration

I then deleted the new migration file

I re-ran add-migration

and then re-ran update-database -verbose

like image 196
James Helms Avatar answered Oct 07 '22 17:10

James Helms