public void Snapshots_compile()
        {
            var codeHelper = new CSharpHelper();
            var generator  = new CSharpMigrationsGenerator(
                codeHelper,
                new CSharpMigrationOperationGenerator(codeHelper),
                new CSharpSnapshotGenerator(codeHelper));

            var model = new Model {
                ["Some:EnumValue"] = RegexOptions.Multiline
            };
            var entityType = model.AddEntityType("Cheese");

            entityType.AddProperty("Pickle", typeof(StringBuilder));

            var modelSnapshotCode = generator.GenerateSnapshot(
                "MyNamespace",
                typeof(MyContext),
                "MySnapshot",
                model);

            Assert.Equal(@"// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design.Tests.Migrations.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace MyNamespace
{
    [DbContext(typeof(CSharpMigrationsGeneratorTest.MyContext))]
    partial class MySnapshot : ModelSnapshot
    {
        protected override void BuildModel(ModelBuilder modelBuilder)
        {
            modelBuilder
                .HasAnnotation(""Some:EnumValue"", RegexOptions.Multiline);

            modelBuilder.Entity(""Cheese"", b =>
                {
                    b.Property<StringBuilder>(""Pickle"");

                    b.ToTable(""Cheese"");
                });
        }
    }
}
", modelSnapshotCode);

            var snapshot = CompileModelSnapshot(modelSnapshotCode, "MyNamespace.MySnapshot");

            Assert.Equal(1, snapshot.Model.GetEntityTypes().Count());
        }
Пример #2
0
        public void Snapshots_compile()
        {
            var codeHelper = new CSharpHelper();
            var generator  = new CSharpMigrationsGenerator(
                codeHelper,
                new CSharpMigrationOperationGenerator(codeHelper),
                new CSharpSnapshotGenerator(codeHelper));

            var modelSnapshotCode = generator.GenerateSnapshot(
                "MyNamespace",
                typeof(MyContext),
                "MySnapshot",
                new Model {
                ["Some:EnumValue"] = RegexOptions.Multiline
            });

            Assert.Equal(@"using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Tools.Core.FunctionalTests.Migrations;
using System.Text.RegularExpressions;

namespace MyNamespace
{
    [DbContext(typeof(CodeCompilationTest.MyContext))]
    partial class MySnapshot : ModelSnapshot
    {
        protected override void BuildModel(ModelBuilder modelBuilder)
        {
            modelBuilder
                .HasAnnotation(""Some:EnumValue"", RegexOptions.Multiline);
        }
    }
}
", modelSnapshotCode);

            var snapshot = CompileModelSnapshot(modelSnapshotCode, "MyNamespace.MySnapshot");

            Assert.Empty(snapshot.Model.GetEntityTypes());
        }
        public void Migrations_compile()
        {
            var codeHelper = new CSharpHelper();
            var generator  = new CSharpMigrationsGenerator(
                codeHelper,
                new CSharpMigrationOperationGenerator(codeHelper),
                new CSharpSnapshotGenerator(codeHelper));

            var migrationCode = generator.GenerateMigration(
                "MyNamespace",
                "MyMigration",
                new MigrationOperation[]
            {
                new SqlOperation
                {
                    Sql = "-- TEST",
                    ["Some:EnumValue"] = RegexOptions.Multiline
                },
                new AlterColumnOperation
                {
                    Name      = "C2",
                    Table     = "T1",
                    ClrType   = typeof(Database),
                    OldColumn = new ColumnOperation
                    {
                        ClrType = typeof(Property)
                    }
                },
                new AddColumnOperation
                {
                    Name    = "C3",
                    Table   = "T1",
                    ClrType = typeof(PropertyEntry)
                }
            },
                new MigrationOperation[0]);

            Assert.Equal(
                @"using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Text.RegularExpressions;

namespace MyNamespace
{
    public partial class MyMigration : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(""-- TEST"")
                .Annotation(""Some:EnumValue"", RegexOptions.Multiline);

            migrationBuilder.AlterColumn<Database>(
                name: ""C2"",
                table: ""T1"",
                nullable: false,
                oldClrType: typeof(Property));

            migrationBuilder.AddColumn<PropertyEntry>(
                name: ""C3"",
                table: ""T1"",
                nullable: false);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {

        }
    }
}
",
                migrationCode);

            var migrationMetadataCode = generator.GenerateMetadata(
                "MyNamespace",
                typeof(MyContext),
                "MyMigration",
                "20150511161616_MyMigration",
                new Model {
                ["Some:EnumValue"] = RegexOptions.Multiline
            });

            Assert.Equal(
                @"using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Design.Tests.Migrations.Design;
using System.Text.RegularExpressions;

namespace MyNamespace
{
    [DbContext(typeof(CSharpMigrationsGeneratorTest.MyContext))]
    [Migration(""20150511161616_MyMigration"")]
    partial class MyMigration
    {
        protected override void BuildTargetModel(ModelBuilder modelBuilder)
        {
            modelBuilder
                .HasAnnotation(""Some:EnumValue"", RegexOptions.Multiline);
        }
    }
}
",
                migrationMetadataCode);

            var build = new BuildSource
            {
                References =
                {
#if NET452
                    BuildReference.ByName("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),
                    BuildReference.ByName("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),
#else
                    BuildReference.ByName("System.Text.RegularExpressions"),
#endif
                    BuildReference.ByName("Microsoft.EntityFrameworkCore.Design.Tests"),
                    BuildReference.ByName("Microsoft.EntityFrameworkCore"),
                    BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational")
                },
                Sources = { migrationCode, migrationMetadataCode }
            };

            var assembly = build.BuildInMemory();

            var migrationType = assembly.GetType("MyNamespace.MyMigration", throwOnError: true, ignoreCase: false);

            var contextTypeAttribute = migrationType.GetTypeInfo().GetCustomAttribute <DbContextAttribute>();

            Assert.NotNull(contextTypeAttribute);
            Assert.Equal(typeof(MyContext), contextTypeAttribute.ContextType);

            var migration = (Migration)Activator.CreateInstance(migrationType);

            Assert.Equal("20150511161616_MyMigration", migration.GetId());

            Assert.Equal(3, migration.UpOperations.Count);
            Assert.Empty(migration.DownOperations);
            Assert.Empty(migration.TargetModel.GetEntityTypes());
        }
        public void Snapshot_with_default_values_are_round_tripped()
        {
            var codeHelper = new CSharpHelper();
            var generator  = new CSharpMigrationsGenerator(
                codeHelper,
                new CSharpMigrationOperationGenerator(codeHelper),
                new CSharpSnapshotGenerator(codeHelper));

            var modelBuilder = new ModelBuilder(new CoreConventionSetBuilder().CreateConventionSet());

            modelBuilder.Entity <EntityWithEveryPrimitive>(eb =>
            {
                eb.Property(e => e.Boolean).HasDefaultValue(false);
                eb.Property(e => e.Byte).HasDefaultValue((byte)0);
                eb.Property(e => e.ByteArray).HasDefaultValue(new byte[] { 0 });
                eb.Property(e => e.Char).HasDefaultValue('0');
                eb.Property(e => e.DateTime).HasDefaultValue(new DateTime(1980, 1, 1));
                eb.Property(e => e.DateTimeOffset).HasDefaultValue(new DateTimeOffset(1980, 1, 1, 0, 0, 0, new TimeSpan(0, 0, 0)));
                eb.Property(e => e.Decimal).HasDefaultValue(0m);
                eb.Property(e => e.Double).HasDefaultValue(0.0);
                eb.Property(e => e.Enum).HasDefaultValue(Enum1.Default);
                eb.Property(e => e.Guid).HasDefaultValue(new Guid());
                eb.Property(e => e.Int16).HasDefaultValue((short)0);
                eb.Property(e => e.Int32).HasDefaultValue(0);
                eb.Property(e => e.Int64).HasDefaultValue(0L);
                eb.Property(e => e.Single).HasDefaultValue((float)0.0);
                eb.Property(e => e.SByte).HasDefaultValue((sbyte)0);
                eb.Property(e => e.String).HasDefaultValue("'\"'");
                eb.Property(e => e.TimeSpan).HasDefaultValue(new TimeSpan(0, 0, 0));
                eb.Property(e => e.UInt16).HasDefaultValue((ushort)0);
                eb.Property(e => e.UInt32).HasDefaultValue(0U);
                eb.Property(e => e.UInt64).HasDefaultValue(0UL);
                eb.Property(e => e.NullableBoolean).HasDefaultValue(true);
                eb.Property(e => e.NullableByte).HasDefaultValue(byte.MaxValue);
                eb.Property(e => e.NullableChar).HasDefaultValue('\'');
                eb.Property(e => e.NullableDateTime).HasDefaultValue(new DateTime(1900, 12, 31));
                eb.Property(e => e.NullableDateTimeOffset).HasDefaultValue(new DateTimeOffset(3000, 1, 1, 0, 0, 0, new TimeSpan(0, 0, 0)));
                eb.Property(e => e.NullableDecimal).HasDefaultValue(2m * long.MaxValue);
                eb.Property(e => e.NullableDouble).HasDefaultValue(0.6822871999174);
                eb.Property(e => e.NullableEnum).HasDefaultValue(Enum1.Default);
                eb.Property(e => e.NullableGuid).HasDefaultValue(new Guid());
                eb.Property(e => e.NullableInt16).HasDefaultValue(short.MinValue);
                eb.Property(e => e.NullableInt32).HasDefaultValue(int.MinValue);
                eb.Property(e => e.NullableInt64).HasDefaultValue(long.MinValue);
                eb.Property(e => e.NullableSingle).HasDefaultValue(0.3333333f);
                eb.Property(e => e.NullableSByte).HasDefaultValue(sbyte.MinValue);
                eb.Property(e => e.NullableTimeSpan).HasDefaultValue(new TimeSpan(-1, 0, 0));
                eb.Property(e => e.NullableUInt16).HasDefaultValue(ushort.MaxValue);
                eb.Property(e => e.NullableUInt32).HasDefaultValue(uint.MaxValue);
                eb.Property(e => e.NullableUInt64).HasDefaultValue(ulong.MaxValue);
            });

            var modelSnapshotCode = generator.GenerateSnapshot(
                "MyNamespace",
                typeof(MyContext),
                "MySnapshot",
                modelBuilder.Model);

            var snapshot   = CompileModelSnapshot(modelSnapshotCode, "MyNamespace.MySnapshot");
            var entityType = snapshot.Model.GetEntityTypes().Single();

            Assert.Equal(typeof(EntityWithEveryPrimitive).FullName, entityType.DisplayName());

            foreach (var property in modelBuilder.Model.GetEntityTypes().Single().GetProperties())
            {
                var snapshotProperty = entityType.FindProperty(property.Name);
                Assert.Equal(property.Relational().DefaultValue, snapshotProperty.Relational().DefaultValue);
            }
        }
Пример #5
0
        /// <summary>
        /// Create and apply the migration for relational database<br/>
        /// 创建并迁移关系数据库中的数据库<br/>
        /// See: https://github.com/aspnet/EntityFramework/blob/master/src/Microsoft.EntityFrameworkCore.Relational/Storage/RelationalDatabaseCreator.cs
        /// </summary>
        /// <param name="context">Entity Framework Core database context</param>
        /// <param name="initialModel">Initial model, only contains migration history</param>
        protected void MigrateRelationalDatabase(DbContext context, IModel initialModel)
        {
            var serviceProvider = ((IInfrastructure <IServiceProvider>)context).Instance;
            // Get the last migration model
            var lastModel     = initialModel;
            var histories     = context.Set <EFCoreMigrationHistory>();
            var lastMigration = histories.OrderByDescending(h => h.Revision).FirstOrDefault();

            if (lastMigration != null)
            {
                // Remove old snapshot code and assembly
                var tempPath = Path.GetTempPath();
                foreach (var file in Directory.EnumerateFiles(
                             tempPath, ModelSnapshotFilePrefix + "*").ToList())
                {
                    try { File.Delete(file); } catch { }
                }
                // Write snapshot code to temp directory and compile it to assembly
                var assemblyName   = ModelSnapshotFilePrefix + DateTime.UtcNow.Ticks;
                var codePath       = Path.Combine(tempPath, assemblyName + ".cs");
                var assemblyPath   = Path.Combine(tempPath, assemblyName + ".dll");
                var compileService = Application.Ioc.Resolve <ICompilerService>();
                var assemblyLoader = Application.Ioc.Resolve <IAssemblyLoader>();
                File.WriteAllText(codePath, lastMigration.Model);
                compileService.Compile(new[] { codePath }, assemblyName, assemblyPath);
                // Load assembly and create the snapshot instance
                var assembly = assemblyLoader.LoadFile(assemblyPath);
                var snapshot = (ModelSnapshot)Activator.CreateInstance(
                    assembly.GetTypes().First(t =>
                                              typeof(ModelSnapshot).IsAssignableFrom(t)));
                lastModel = snapshot.Model;
            }
            // Compare with the newest model
            var modelDiffer     = serviceProvider.GetService <IMigrationsModelDiffer>();
            var sqlGenerator    = serviceProvider.GetService <IMigrationsSqlGenerator>();
            var commandExecutor = serviceProvider.GetService <IMigrationCommandExecutor>();
            var operations      = modelDiffer.GetDifferences(lastModel, context.Model);

            if (operations.Count <= 0)
            {
                // There no difference
                return;
            }
            // There some difference, we need perform the migration
            var commands   = sqlGenerator.Generate(operations, context.Model);
            var connection = serviceProvider.GetService <IRelationalConnection>();
            // Take a snapshot to the newest model
            var codeHelper = new CSharpHelper();
            var generator  = new CSharpMigrationsGenerator(
                new MigrationsCodeGeneratorDependencies(),
                new CSharpMigrationsGeneratorDependencies(
                    codeHelper,
                    new CSharpMigrationOperationGenerator(
                        new CSharpMigrationOperationGeneratorDependencies(codeHelper)),
                    new CSharpSnapshotGenerator(new CSharpSnapshotGeneratorDependencies(codeHelper))));
            var modelSnapshot = generator.GenerateSnapshot(
                ModelSnapshotNamespace, context.GetType(),
                ModelSnapshotClassPrefix + DateTime.UtcNow.Ticks, context.Model);
            // Insert the history first, if migration failed, delete it
            var history = new EFCoreMigrationHistory(modelSnapshot);

            histories.Add(history);
            context.SaveChanges();
            try {
                // Execute migration commands
                commandExecutor.ExecuteNonQuery(commands, connection);
            } catch {
                histories.Remove(history);
                context.SaveChanges();
                throw;
            }
        }
        protected void Test(Action <ModelBuilder> buildModel, string expectedCode, Action <IModel> assert)
        {
            var modelBuilder = CreateConventionalModelBuilder();

            modelBuilder.HasChangeTrackingStrategy(ChangeTrackingStrategy.Snapshot);
            buildModel(modelBuilder);

            var typeMappingSource = new MySqlTypeMappingSource(
                TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>(),
                TestServiceFactory.Instance.Create <MySqlOptions>(),
                TestServiceFactory.Instance.Create <MySqlConnectionInfo>());

            var modelValidator = CreateModelValidator(typeMappingSource);

            modelValidator.Validate(
                modelBuilder.Model,
                new DiagnosticsLogger <DbLoggerCategory.Model.Validation>(
                    new ListLoggerFactory(category => category == DbLoggerCategory.Model.Validation.Name),
                    new LoggingOptions(),
                    new DiagnosticListener("Fake"),
                    new MySqlLoggingDefinitions()));

            var model = modelBuilder.Model;

            var codeHelper = new CSharpHelper(
                typeMappingSource);

            var generator = new CSharpMigrationsGenerator(
                new MigrationsCodeGeneratorDependencies(typeMappingSource),
                new CSharpMigrationsGeneratorDependencies(
                    codeHelper,
                    new CSharpMigrationOperationGenerator(
                        new CSharpMigrationOperationGeneratorDependencies(
                            codeHelper)),
                    new CSharpSnapshotGenerator(
                        new CSharpSnapshotGeneratorDependencies(
                            codeHelper,
                            typeMappingSource))));

            var code = generator.GenerateSnapshot("RootNamespace", typeof(DbContext), "Snapshot", model);

            Assert.Equal(expectedCode, code, ignoreLineEndingDifferences: true);

            var build = new BuildSource
            {
                Sources =
                {
                    @"
                    using System;
                    using Microsoft.EntityFrameworkCore;
                    using Microsoft.EntityFrameworkCore.Metadata;
                    using Microsoft.EntityFrameworkCore.Metadata.Conventions;
                    using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

                    public static class ModelSnapshot
                    {
                        public static IModel Model
                        {
                            get
                            {
                                var builder = new ModelBuilder(new ConventionSet());
                                " + code + @"

                                return builder.Model;
                            }
                        }
                   }
                "
                }
            };

            foreach (var buildReference in GetReferences())
            {
                build.References.Add(buildReference);
            }

            var assembly    = build.BuildInMemory();
            var factoryType = assembly.GetType("ModelSnapshot");
            var property    = factoryType.GetTypeInfo().GetDeclaredProperty("Model");
            var value       = (IModel)property.GetValue(null);

            Assert.NotNull(value);
            assert(value);
        }
Пример #7
0
        public void Migrations_compile()
        {
            var codeHelper = new CSharpHelper();
            var generator  = new CSharpMigrationsGenerator(
                codeHelper,
                new CSharpMigrationOperationGenerator(codeHelper),
                new CSharpSnapshotGenerator(codeHelper));

            var migrationCode = generator.GenerateMigration(
                "MyNamespace",
                "MyMigration",
                new[] {
                new SqlOperation
                {
                    Sql = "-- TEST",
                    ["Some:EnumValue"] = RegexOptions.Multiline
                }
            },
                new MigrationOperation[0]);

            Assert.Equal(
                @"using System;
using System.Collections.Generic;
using Microsoft.Data.Entity.Migrations;
using System.Text.RegularExpressions;

namespace MyNamespace
{
    public partial class MyMigration : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql(""-- TEST"")
                .Annotation(""Some:EnumValue"", RegexOptions.Multiline);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {

        }
    }
}
",
                migrationCode);

            var migrationMetadataCode = generator.GenerateMetadata(
                "MyNamespace",
                typeof(MyContext),
                "MyMigration",
                "20150511161616_MyMigration",
                new Model {
                ["Some:EnumValue"] = RegexOptions.Multiline
            });

            Assert.Equal(
                @"using System;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Commands.Migrations;
using System.Text.RegularExpressions;

namespace MyNamespace
{
    [DbContext(typeof(CodeCompilationTest.MyContext))]
    [Migration(""20150511161616_MyMigration"")]
    partial class MyMigration
    {
        protected override void BuildTargetModel(ModelBuilder modelBuilder)
        {
            modelBuilder
                .HasAnnotation(""Some:EnumValue"", RegexOptions.Multiline);
        }
    }
}
",
                migrationMetadataCode);

            var build = new BuildSource
            {
                References =
                {
                    BuildReference.ByName(typeof(CodeCompilationTest).GetTypeInfo().Assembly.GetName().Name),
#if DNXCORE50
                    BuildReference.ByName("System.Text.RegularExpressions"),
#else
                    BuildReference.ByName("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),
                    BuildReference.ByName("System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
#endif
                    BuildReference.ByName("EntityFramework.Core"),
                    BuildReference.ByName("EntityFramework.Relational")
                },
                Sources = { migrationCode, migrationMetadataCode }
            };

            var assembly = build.BuildInMemory();

            var migrationType = assembly.GetType("MyNamespace.MyMigration", throwOnError: true, ignoreCase: false);

            var contextTypeAttribute = migrationType.GetTypeInfo().GetCustomAttribute <DbContextAttribute>();

            Assert.NotNull(contextTypeAttribute);
            Assert.Equal(typeof(MyContext), contextTypeAttribute.ContextType);

            var migration = (Migration)Activator.CreateInstance(migrationType);

            Assert.Equal("20150511161616_MyMigration", migration.GetId());

            Assert.Equal(1, migration.UpOperations.Count);
            Assert.Empty(migration.DownOperations);
            Assert.Empty(migration.TargetModel.GetEntityTypes());
        }
Пример #8
0
        public void Snapshots_compile()
        {
            var codeHelper = new CSharpHelper();
            var generator  = new CSharpMigrationsGenerator(
                codeHelper,
                new CSharpMigrationOperationGenerator(codeHelper),
                new CSharpSnapshotGenerator(codeHelper));

            var modelSnapshotCode = generator.GenerateSnapshot(
                "MyNamespace",
                typeof(MyContext),
                "MySnapshot",
                new Model {
                ["Some:EnumValue"] = RegexOptions.Multiline
            });

            Assert.Equal(@"using System;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Commands.Migrations;
using System.Text.RegularExpressions;

namespace MyNamespace
{
    [DbContext(typeof(CodeCompilationTest.MyContext))]
    partial class MySnapshot : ModelSnapshot
    {
        protected override void BuildModel(ModelBuilder modelBuilder)
        {
            modelBuilder
                .HasAnnotation(""Some:EnumValue"", RegexOptions.Multiline);
        }
    }
}
", modelSnapshotCode);

            var build = new BuildSource
            {
                References =
                {
                    BuildReference.ByName(typeof(CodeCompilationTest).GetTypeInfo().Assembly.GetName().Name),
#if DNXCORE50
                    BuildReference.ByName("System.Text.RegularExpressions"),
#else
                    BuildReference.ByName("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),
                    BuildReference.ByName("System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
#endif
                    BuildReference.ByName("EntityFramework.Core"),
                    BuildReference.ByName("EntityFramework.Relational")
                },
                Sources = { modelSnapshotCode }
            };

            var assembly = build.BuildInMemory();

            var snapshotType = assembly.GetType("MyNamespace.MySnapshot", throwOnError: true, ignoreCase: false);

            var contextTypeAttribute = snapshotType.GetTypeInfo().GetCustomAttribute <DbContextAttribute>();

            Assert.NotNull(contextTypeAttribute);
            Assert.Equal(typeof(MyContext), contextTypeAttribute.ContextType);

            var snapshot = (ModelSnapshot)Activator.CreateInstance(snapshotType);

            Assert.Empty(snapshot.Model.GetEntityTypes());
        }
Пример #9
0
        public void Migrate(string MigrateName, Action <ModelBuilder> OnModelCreating, bool ObjectNameLowerCase = false)
        {
            EnsureCreated();

            using (var context = new DbContext(DbOptions.DbOptions))
            {
                var serviceProvider = ((IInfrastructure <IServiceProvider>)context).Instance;
                var databaseCreator = serviceProvider.GetService <IDatabaseCreator>();
                if (databaseCreator is IRelationalDatabaseCreator)
                {
                    var newModel      = InitModel(serviceProvider, OnModelCreating, context);
                    var lastModel     = newModel;
                    var DHContent     = GetHistory();
                    var lastMigration = DHContent.History.Where(w => w.MigrateName.Equals(MigrateName)).OrderByDescending(h => h.Revision).FirstOrDefault();
                    if (lastMigration != null)
                    {
                        DelTempFile();
                        var tempPath     = Path.GetTempPath();
                        var assemblyName = ModelSnapshotFilePrefix + DateTime.UtcNow.Ticks;
                        var codePath     = Path.Combine(tempPath, assemblyName + ".cs");
                        var assemblyPath = Path.Combine(tempPath, assemblyName + ".dll");
                        File.WriteAllText(codePath, lastMigration.Model);

                        Compile(new[] { codePath }, assemblyName, assemblyPath);

                        var assembly = Assembly.LoadFile(assemblyPath);
                        var snapshot = (ModelSnapshot)Activator.CreateInstance(
                            assembly.GetTypes().First(t =>
                                                      typeof(ModelSnapshot).GetTypeInfo().IsAssignableFrom(t)));
                        lastModel = snapshot.Model;
                    }
                    else
                    {
                        lastModel = null;
                    }


                    var modelDiffer = serviceProvider.GetService <IMigrationsModelDiffer>();
                    var sqlGeneratorDependencies = serviceProvider.GetService <MigrationsSqlGeneratorDependencies>();
                    var sqlGenerator             = serviceProvider.GetService <IMigrationsSqlGenerator>(); // new Xakep.EntityFrameworkCore.Migrations.MigrationsSqlGenerator(sqlGeneratorDependencies, ObjectNameLowerCase);

                    var operations = modelDiffer.GetDifferences(lastModel, newModel);
                    if (operations.Count <= 0)
                    {
                        return;
                    }
                    var commands = sqlGenerator.Generate(operations, newModel);

                    var connection      = serviceProvider.GetService <IRelationalConnection>();
                    var commandExecutor = serviceProvider.GetService <IMigrationCommandExecutor>();


                    var codeHelper = new CSharpHelper();
                    var generator  = new CSharpMigrationsGenerator(
                        new MigrationsCodeGeneratorDependencies(),
                        new CSharpMigrationsGeneratorDependencies(codeHelper,
                                                                  new CSharpMigrationOperationGenerator(new CSharpMigrationOperationGeneratorDependencies(codeHelper)),
                                                                  new CSharpSnapshotGenerator(new CSharpSnapshotGeneratorDependencies(codeHelper))
                                                                  )
                        );


                    var modelSnapshot = generator.GenerateSnapshot(
                        ModelSnapshotNamespace, context.GetType(),
                        ModelSnapshotClassPrefix + DateTime.UtcNow.Ticks, newModel);


                    var history = new MigrateTableHistory(MigrateName, modelSnapshot);
                    DHContent.History.Add(history);
                    DHContent.SaveChanges();
                    try
                    {
                        commandExecutor.ExecuteNonQuery(commands, connection);
                    }
                    catch
                    {
                        DHContent.Remove(history);
                        DHContent.SaveChanges();
                    }
                    finally
                    {
                        if (lastModel != null)
                        {
                            DelTempFile();
                        }
                    }
                }
            }
        }