实体框架 - 尝试过于频繁地创建MigrationHistory表

时间:2012-09-05 10:44:25

标签: entity-framework

实体框架迁移似乎想要在相当多的时间内创建迁移历史记录(并非每次都是,通常在需要制作新表时)。问题是它存在并且似乎很高兴能够跟上国家的步伐。我认为默认不应该这样做......?

我有一个以EF 4.3开头的数据库,稍微更新了EF 5.0RC,现在是最终版本EF 5.

使用以下命令创建迁移:update-database -Script 因为从4.3到5.x命名外键约束的惯例存在问题所以我偶尔需要摆弄一些alter脚本。

即。这是最新的更新:

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')

如果我将迁移文件添加到项目中,则create table语句将消失。可能是因为我经常评论以前的迁移文件吗?因为我现在更有可能重新创建数据库/复制不同版本的数据库。

迁移历史记录表 - 目前此表中只有自动迁移记录。

回到这个问题。为什么它正在尝试创建迁移历史记录表,而此表已经存在且已从中获取状态。

谢谢, 马特

0 个答案:

没有答案