private void GenerateAndValidateCode(MigrationInfo migration) { var @namespace = GetType().Namespace + ".DynamicallyCompiled"; var className = "Migration" + Guid.NewGuid().ToString("N"); var generator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator()); var migrationBuilder = new IndentedStringBuilder(); var migrationMetadataBuilder = new IndentedStringBuilder(); generator.GenerateMigrationClass(@namespace, className, migration, migrationBuilder); generator.GenerateMigrationMetadataClass(@namespace, className, migration, typeof(DbContext), migrationMetadataBuilder); var migrationSource = migrationBuilder.ToString(); var migrationMetadataSource = migrationMetadataBuilder.ToString(); Assembly.Load("System.Linq.Expressions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"); var compiledAssembly = CodeGeneratorTestHelper.Compile( @namespace + ".dll", new[] { migrationSource, migrationMetadataSource }, new[] { "mscorlib", "System.Core", "System.Linq.Expressions", "System.Runtime", "EntityFramework.Core", "EntityFramework.Relational", "EntityFramework.Migrations" }); var compiledMigration = new MigrationInfo( (Migration)compiledAssembly.CreateInstance(@namespace + "." + className)); generator = new CSharpMigrationCodeGenerator(new CSharpModelCodeGenerator()); migrationBuilder = new IndentedStringBuilder(); migrationMetadataBuilder = new IndentedStringBuilder(); generator.GenerateMigrationClass(@namespace, className, compiledMigration, migrationBuilder); generator.GenerateMigrationMetadataClass(@namespace, className, compiledMigration, typeof(DbContext), migrationMetadataBuilder); Assert.Equal(migrationSource, migrationBuilder.ToString()); Assert.Equal(migrationMetadataSource, migrationMetadataBuilder.ToString()); }
private void GenerateAndValidateCode(IModel model) { var @namespace = GetType().Namespace + ".DynamicallyCompiled"; var className = "ModelSnapshot" + Guid.NewGuid().ToString("N"); var generator = new CSharpModelCodeGenerator(); var modelSnapshotBuilder = new IndentedStringBuilder(); generator.GenerateModelSnapshotClass(@namespace, className, model, typeof(DbContext), modelSnapshotBuilder); var modelSnapshotSource = modelSnapshotBuilder.ToString(); var compiledAssembly = CodeGeneratorTestHelper.Compile( @namespace + ".dll", new[] { modelSnapshotSource }, new[] { "mscorlib", "System.Runtime", "EntityFramework.Core", "EntityFramework.Migrations" }); var compiledModelSnapshot = (ModelSnapshot) compiledAssembly.CreateInstance(@namespace + "." + className); Assert.NotNull(compiledModelSnapshot); var compiledModel = (IModel)compiledModelSnapshot.GetType().GetProperty("Model").GetValue(compiledModelSnapshot); Assert.NotNull(compiledModel); generator = new CSharpModelCodeGenerator(); modelSnapshotBuilder = new IndentedStringBuilder(); generator.GenerateModelSnapshotClass(@namespace, className, compiledModel, typeof(DbContext), modelSnapshotBuilder); Assert.Equal(modelSnapshotSource, modelSnapshotBuilder.ToString()); }