بازسازی جدول MigrationHistory با کد نویسی در EF Code first
نویسنده: وحید نصیری
تاریخ: ۱۳۹۲/۰۸/۱۳ ۸:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
enable-migrations add-migration Initial -IgnoreChanges update-database
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.Design;
namespace EFTests
{
/// <summary>
/// Using Entity Framework Code First with an existing database.
/// </summary>
public static class CreateMigrationHistory
{
/// <summary>
/// Creates a new '__MigrationHistory' table.
/// Enables migrations using Entity Framework Code First on an existing database.
/// </summary>
public static void UpdateDatabase(DbMigrationsConfiguration configuration)
{
var scaffolder = new MigrationScaffolder(configuration);
// Creates an empty migration, so that the future migrations will start from the current state of your database.
var scaffoldedMigration = scaffolder.Scaffold("IgnoreChanges", ignoreChanges: true);
// enable-migrations
// add-migration Initial -IgnoreChanges
configuration.MigrationsAssembly = new MigrationCompiler(ProgrammingLanguage.CSharp.ToString())
.Compile(configuration.MigrationsNamespace, scaffoldedMigration);
// update-database
var dbMigrator = new DbMigrator(configuration);
dbMigrator.Update();
}
}
} using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.Design;
using System.Data.Entity.Spatial;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Resources;
using System.Text;
namespace EF_General.Models.Ex22
{
public enum ProgrammingLanguage
{
CSharp,
VB
}
public class MigrationCompiler
{
private readonly CodeDomProvider _codeProvider;
public MigrationCompiler(string language)
{
_codeProvider = CodeDomProvider.CreateProvider(language);
}
public Assembly Compile(string @namespace, params ScaffoldedMigration[] scaffoldedMigrations)
{
var options = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true
};
options.ReferencedAssemblies.Add(typeof(string).Assembly.Location);
options.ReferencedAssemblies.Add(typeof(Expression).Assembly.Location);
options.ReferencedAssemblies.Add(typeof(DbMigrator).Assembly.Location);
options.ReferencedAssemblies.Add(typeof(DbContext).Assembly.Location);
options.ReferencedAssemblies.Add(typeof(DbConnection).Assembly.Location);
options.ReferencedAssemblies.Add(typeof(Component).Assembly.Location);
options.ReferencedAssemblies.Add(typeof(MigrationCompiler).Assembly.Location);
options.ReferencedAssemblies.Add(typeof(DbGeography).Assembly.Location);
var embededResources = GenerateEmbeddedResources(scaffoldedMigrations, @namespace);
foreach (var resource in embededResources)
options.EmbeddedResources.Add(resource);
var sources = scaffoldedMigrations.SelectMany(g => new[] { g.UserCode, g.DesignerCode });
var compilerResults = _codeProvider.CompileAssemblyFromSource(options, sources.ToArray());
foreach (var resource in embededResources)
File.Delete(resource);
if (compilerResults.Errors.Count > 0)
{
throw new InvalidOperationException(BuildCompileErrorMessage(compilerResults.Errors));
}
return compilerResults.CompiledAssembly;
}
private static string BuildCompileErrorMessage(CompilerErrorCollection errors)
{
var stringBuilder = new StringBuilder();
foreach (CompilerError error in errors)
{
stringBuilder.AppendLine(error.ToString());
}
return stringBuilder.ToString();
}
private static IEnumerable<string> GenerateEmbeddedResources(IEnumerable<ScaffoldedMigration> scaffoldedMigrations, string @namespace)
{
foreach (var scaffoldedMigration in scaffoldedMigrations)
{
var className = GetClassName(scaffoldedMigration.MigrationId);
var embededResource = Path.Combine(
Path.GetTempPath(),
@namespace + "." + className + ".resources");
using (var writer = new ResourceWriter(embededResource))
{
foreach (var resource in scaffoldedMigration.Resources)
writer.AddResource(resource.Key, resource.Value);
}
yield return embededResource;
}
}
private static string GetClassName(string migrationId)
{
return migrationId
.Split(new[] { '_' }, 2)
.Last()
.Replace(" ", string.Empty);
}
}
} CreateMigrationHistory.UpdateDatabase(new Configuration());
public class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
} using System.Data.Entity.Spatial; options.ReferencedAssemblies.Add(typeof(DbGeography).Assembly.Location); scaffoldedMigration.Resources
var scaffolder = new MigrationScaffolder(configuration);