Re-create table with Entity Framework 5 and nuget

后端 未结 3 761
离开以前
离开以前 2020-12-28 08:52

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

相关标签:
3条回答
  • 2020-12-28 09:34

    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

    0 讨论(0)
  • 2020-12-28 09:37

    You could just drop the database then from the package manager console run the 'update-database' command, all would be recreated including whatever updates you made.

    0 讨论(0)
  • 2020-12-28 09:38

    There are a few options I keep in my arsenal for code-first migrations and they depend on why you need to delete tables or delete the records. Here is my methods:

    • If you modified the models and the mappings are causing an error that prevents you from not being able to update the tables you could Delete the entire database using SQL Management Server Studio & Delete the Migration folder You may want to save a script to re-populate your test data using an sql script or save your Configuration.cs file and when you execute the update database command the data will be re-populated.

      Here is an example of script for a stored procedure to just drop table data:

      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 ?'
      
    • If you just want to drop the data you can use the package manager console command: PM> Update-Database -TargetMigration $InitialDatabase & delete the migration file created ie: '201502210525393_example_with_error.cs' and re-run 'Add-Migration new_example.cs' again. This puts the database back to its initial snapshot

    • Or you could use your method: delete the row in [dbo].[__MigrationHistory] & migration file ie: '201502210525393_example_with_error.cs' then re-run add-migration and update-database

    0 讨论(0)
提交回复
热议问题