Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework - Trying to make MigrationHistory table too often
    text
    copied!<p>Entity Framework migrations seems to want to want to create the Migration History quite a bit of the time (not every time, normally when a new table needs making). The problem is it that it exists and seems fairly happy at keeping up with the state. I presume it should not be doing this by default... ?</p> <p>I have a database which started with EF 4.3, updated a little with EF 5.0RC and is now on final version EF 5. </p> <p>The migrations are created using : update-database -Script as there had been problems with the convention of naming foreign key constraints from 4.3 to 5.x so I will occasionally need to fiddle with some of the alter scripts. </p> <p>ie. This is the newest update: </p> <pre><code>ALTER TABLE [dbo].[PermissionRecords] DROP CONSTRAINT [FK_dbo.PermissionRecords_dbo.Roles_Role_Id] ALTER TABLE [dbo].[PermissionRecords] DROP CONSTRAINT [FK_dbo.PermissionRecords_dbo.Roles_Role_Id1] DROP INDEX [IX_Role_Id] ON [dbo].[PermissionRecords] DROP INDEX [IX_Role_Id1] ON [dbo].[PermissionRecords] CREATE TABLE [dbo].[RolesEffectivePermissions] ( [Role_Id] [int] NOT NULL, [PermissionRecord_Id] [int] NOT NULL, CONSTRAINT [PK_dbo.RolesEffectivePermissions] PRIMARY KEY ([Role_Id], [PermissionRecord_Id]) ) CREATE TABLE [dbo].[RolesGivenPermissions] ( [Role_Id] [int] NOT NULL, [PermissionRecord_Id] [int] NOT NULL, CONSTRAINT [PK_dbo.RolesGivenPermissions] PRIMARY KEY ([Role_Id], [PermissionRecord_Id]) ) CREATE INDEX [IX_Role_Id] ON [dbo].[RolesEffectivePermissions]([Role_Id]) CREATE INDEX [IX_PermissionRecord_Id] ON [dbo].[RolesEffectivePermissions]([PermissionRecord_Id]) CREATE INDEX [IX_Role_Id] ON [dbo].[RolesGivenPermissions]([Role_Id]) CREATE INDEX [IX_PermissionRecord_Id] ON [dbo].[RolesGivenPermissions]([PermissionRecord_Id]) DECLARE @var0 nvarchar(128) SELECT @var0 = name FROM sys.default_constraints WHERE parent_object_id = object_id(N'dbo.PermissionRecords') AND col_name(parent_object_id, parent_column_id) = 'Role_Id'; IF @var0 IS NOT NULL EXECUTE('ALTER TABLE [dbo].[PermissionRecords] DROP CONSTRAINT ' + @var0) ALTER TABLE [dbo].[PermissionRecords] DROP COLUMN [Role_Id] DECLARE @var1 nvarchar(128) SELECT @var1 = name FROM sys.default_constraints WHERE parent_object_id = object_id(N'dbo.PermissionRecords') AND col_name(parent_object_id, parent_column_id) = 'Role_Id1'; IF @var1 IS NOT NULL EXECUTE('ALTER TABLE [dbo].[PermissionRecords] DROP CONSTRAINT ' + @var1) ALTER TABLE [dbo].[PermissionRecords] DROP COLUMN [Role_Id1] ALTER TABLE [dbo].[RolesEffectivePermissions] ADD CONSTRAINT [FK_dbo.RolesEffectivePermissions_dbo.Roles_Role_Id] FOREIGN KEY ([Role_Id]) REFERENCES [dbo].[Roles] ([Id]) ON DELETE CASCADE ALTER TABLE [dbo].[RolesEffectivePermissions] ADD CONSTRAINT [FK_dbo.RolesEffectivePermissions_dbo.PermissionRecords_PermissionRecord_Id] FOREIGN KEY ([PermissionRecord_Id]) REFERENCES [dbo].[PermissionRecords] ([Id]) ON DELETE CASCADE ALTER TABLE [dbo].[RolesGivenPermissions] ADD CONSTRAINT [FK_dbo.RolesGivenPermissions_dbo.Roles_Role_Id] FOREIGN KEY ([Role_Id]) REFERENCES [dbo].[Roles] ([Id]) ON DELETE CASCADE ALTER TABLE [dbo].[RolesGivenPermissions] ADD CONSTRAINT [FK_dbo.RolesGivenPermissions_dbo.PermissionRecords_PermissionRecord_Id] FOREIGN KEY ([PermissionRecord_Id]) REFERENCES [dbo].[PermissionRecords] ([Id]) ON DELETE CASCADE CREATE TABLE [dbo].[__MigrationHistory] ( [MigrationId] [nvarchar](255) NOT NULL, [Model] [varbinary](max) NOT NULL, [ProductVersion] [nvarchar](32) NOT NULL, CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId]) ) BEGIN TRY EXEC sp_MS_marksystemobject 'dbo.__MigrationHistory' END TRY BEGIN CATCH END CATCH INSERT INTO [__MigrationHistory] ([MigrationId], [Model], [ProductVersion]) VALUES ('201209050951363_AutomaticMigration', 0x1F8B0800000000000400ED7DDB721C ..., '5.0.0.net40') </code></pre> <p>If I add a migration file to the project the create table statement disappears. Could it be because I often comment out previous migration files? As Im far more likely to recreate the database / copy a different version of the database at the moment. </p> <p>Migration history table - at the moment there is only automatic migrations records in this table. </p> <p>So back to the question. Why is is that it is trying to create the Migration History table while this table already exists and has got the state from it. </p> <p>Thanks, Matt </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload