بررسی ساختار جدول MigrationHistory در Entity Framework 6.x
نویسنده: وحید نصیری
تاریخ: ۱۳۹۴/۰۹/۱۵ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.IO.Compression;
using System.Xml.Linq;
namespace EF_General
{
public static class InsideMigrations
{
public static void PrintFirstMigrationModel()
{
const string connectionString = "Data Source=(local);Initial Catalog=TestDbIdentity;Integrated Security = true";
const string sqlToExecute = "select top 1 model from __MigrationHistory";
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (var command = new SqlCommand(sqlToExecute, connection))
{
using (var reader = command.ExecuteReader())
{
if (!reader.HasRows)
{
throw new KeyNotFoundException("Nothing to display.");
}
while (reader.Read())
{
var model = (byte[]) reader["model"];
var decompressed = decompressMigrationModel(model);
Console.WriteLine(decompressed);
}
}
}
}
}
private static XDocument decompressMigrationModel(byte[] bytes)
{
using (var memoryStream = new MemoryStream(bytes))
{
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
return XDocument.Load(gzipStream);
}
}
}
}
}