我不得不重构一个现有的数据库,在这里我手动修改了主键并添加了外键。在生产数据库中,我使用SQL设计器手动进行了此操作,然后使用EF6 Code First创建了更改,并尝试修改__MigrationHistory表。
我使用了SQL结构比较工具(SQL DBDiff 5.5),发现空的生成数据库和带有数据的生产数据库之间没有区别。
现在,我提取新数据库的MigrationHistory表以使用以下命令将其复制到旧数据库中:
use NewDB
go
DECLARE @bin VARBINARY(MAX)
set @bin = (select model from [__MigrationHistory] where MigrationId = '201904181338010_AutomaticMigration')
print @bin
并将其添加到旧数据库中。
use OldDB
go
truncate table [__MigrationHistory]
insert into [__MigrationHistory] (MigrationId, ContextKey, Model, ProductVersion) values ('201904181338010_AutomaticMigration',
'Test_Database.WebContext',
0x01F8B0800000000000400EDBDDB72E, '6.2.0-61023')
(当然会缩短值:-)
如果我现在开始我的代码,它会出于某种原因尝试迁移并得到以下错误:
System.IO.InvalidDataException: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
at System.IO.Compression.GZipDecoder.ReadHeader(InputBuffer input)
at System.IO.Compression.Inflater.Decode()
at System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length)
at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding)
at System.Xml.XmlTextReaderImpl.FinishInitStream()
at System.Xml.XmlReaderSettings.CreateReader(Stream input, Uri baseUri, String baseUriString, XmlParserContext inputContext)
at System.Xml.Linq.XDocument.Load(Stream stream, LoadOptions options)
at System.Data.Entity.Migrations.Edm.ModelCompressor.Decompress(Byte[] bytes)
at System.Data.Entity.Migrations.History.HistoryRepository.GetLastModel(String& migrationId, String& productVersion, String contextKey)
at System.Data.Entity.Migrations.DbMigrator.GetLastModel(DbMigration lastMigration, String currentMigrationId)
at System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model, DbMigration lastMigration)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
我也尝试过
CONVERT(VARBINARY(max),
0x1F8B0...)
这是RedGate SQL Data Compare 13.7生成的
BEGIN TRANSACTION
PRINT(N'Update 1 row in [dbo].[__MigrationHistory]')
UPDATE [dbo].[__MigrationHistory] SET [Model]=0x01f8b
删除表__MigrationHistory强制EF重新创建所有现有表...
有什么想法吗?