private ModelSnapshot CompileModelSnapshot(string modelSnapshotCode, string modelSnapshotTypeName) { var build = new BuildSource { References = { #if NET452 BuildReference.ByName("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"), BuildReference.ByName("System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), #else BuildReference.ByName("System.Text.RegularExpressions"), #endif BuildReference.ByName("Microsoft.EntityFrameworkCore.Design.Tests"), BuildReference.ByName("Microsoft.EntityFrameworkCore"), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational") }, Sources = { modelSnapshotCode } }; var assembly = build.BuildInMemory(); var snapshotType = assembly.GetType(modelSnapshotTypeName, throwOnError: true, ignoreCase: false); var contextTypeAttribute = snapshotType.GetTypeInfo().GetCustomAttribute <DbContextAttribute>(); Assert.NotNull(contextTypeAttribute); Assert.Equal(typeof(MyContext), contextTypeAttribute.ContextType); return((ModelSnapshot)Activator.CreateInstance(snapshotType)); }
protected void Test( Action <ModelBuilder> buildModel, ModelCodeGenerationOptions options, Action <ScaffoldedModel> assertScaffold, Action <IModel> assertModel) { var modelBuilder = SqlServerTestHelpers.Instance.CreateConventionBuilder(); modelBuilder.Model.RemoveAnnotation(CoreAnnotationNames.ProductVersion); buildModel(modelBuilder); var model = modelBuilder.FinalizeModel(); var services = new ServiceCollection() .AddEntityFrameworkDesignTimeServices(); new SqlServerDesignTimeServices().ConfigureDesignTimeServices(services); var generator = services .BuildServiceProvider() .GetRequiredService <IModelCodeGenerator>(); options.ModelNamespace ??= "TestNamespace"; options.ContextName = "TestDbContext"; options.ConnectionString = "Initial Catalog=TestDatabase"; var scaffoldedModel = generator.GenerateModel( model, options); assertScaffold(scaffoldedModel); var build = new BuildSource { References = { BuildReference.ByName("Microsoft.EntityFrameworkCore.Abstractions"), BuildReference.ByName("Microsoft.EntityFrameworkCore"), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational"), BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer") }, Sources = new List <string>( new[] { scaffoldedModel.ContextFile.Code }.Concat( scaffoldedModel.AdditionalFiles.Select(f => f.Code))) }; var assembly = build.BuildInMemory(); var contextNamespace = options.ContextNamespace ?? options.ModelNamespace; var context = (DbContext)assembly.CreateInstance( !string.IsNullOrEmpty(contextNamespace) ? contextNamespace + "." + options.ContextName : options.ContextName); if (assertModel != null) { var compiledModel = context.Model; assertModel(compiledModel); } }
private void Test(Action <ModelBuilder> buildModel, string expectedCode, Action <IModel> assert) { var modelBuilder = TestHelpers.Instance.CreateConventionBuilder(); buildModel(modelBuilder); var model = modelBuilder.Model; var generator = new CSharpModelGenerator(new CSharpHelper()); var builder = new IndentedStringBuilder(); generator.Generate(model, builder); var code = builder.ToString(); Assert.Equal(expectedCode, code); var build = new BuildSource { References = { BuildReference.ByName("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"), BuildReference.ByName("System.Linq.Expressions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), BuildReference.ByName("System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), BuildReference.ByName("EntityFramework.Core"), BuildReference.ByName("EntityFramework.Relational") }, Sources = { @" using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Metadata.Conventions; using Microsoft.Data.Entity.Migrations.Infrastructure; public static class ModelSnapshot { public static IModel Model { get { var builder = new ModelBuilder(new ConventionSet()); " + code + @" return builder.Model; } } } " } }; var assembly = build.BuildInMemory(); var factoryType = assembly.GetType("ModelSnapshot"); var property = factoryType.GetProperty("Model"); var value = (IModel)property.GetValue(null); Assert.NotNull(value); assert(value); }
public void Snapshots_compile() { var codeHelper = new CSharpHelper(); var generator = new CSharpMigrationGenerator( codeHelper, new CSharpMigrationOperationGenerator(codeHelper), new CSharpModelGenerator(codeHelper)); var modelSnapshotCode = generator.GenerateSnapshot("MyNamespace", typeof(MyContext), "MySnapshot", new Model()); Assert.Equal(@"using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Relational.Migrations.Infrastructure; using Microsoft.Data.Entity.Commands.Migrations; namespace MyNamespace { [ContextType(typeof(CodeCompilationTest.MyContext))] partial class MySnapshot : ModelSnapshot { public override void BuildModel(ModelBuilder builder) { } } } ", modelSnapshotCode); var build = new BuildSource { References = { BuildReference.ByName(typeof(CodeCompilationTest).Assembly.GetName().Name), BuildReference.ByName("System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), BuildReference.ByName("EntityFramework.Core"), BuildReference.ByName("EntityFramework.Relational") }, Sources = { modelSnapshotCode } }; var assembly = build.BuildInMemory(); var snapshotType = assembly.GetType("MyNamespace.MySnapshot", throwOnError: true); var contextTypeAttribute = snapshotType.GetCustomAttribute <ContextTypeAttribute>(); Assert.NotNull(contextTypeAttribute); Assert.Equal(typeof(MyContext), contextTypeAttribute.ContextType); var snapshot = (ModelSnapshot)Activator.CreateInstance(snapshotType); var conventions = new ConventionSet(); var modelBuilder = new ModelBuilder(conventions); snapshot.BuildModel(modelBuilder); Assert.Empty(modelBuilder.Model.EntityTypes); }
protected void Test( Action <ModelBuilder> buildModel, ModelCodeGenerationOptions options, Action <ScaffoldedModel> assertScaffold, Action <IModel> assertModel) { var modelBuilder = new ModelBuilder(BuildNonValidatingConventionSet()); modelBuilder.Model.RemoveAnnotation(CoreAnnotationNames.ProductVersionAnnotation); buildModel(modelBuilder); var _ = modelBuilder.Model.Scaffolding().EntityTypeErrors; var model = modelBuilder.FinalizeModel(); var services = new ServiceCollection() .AddEntityFrameworkDesignTimeServices(); new SqlServerDesignTimeServices().ConfigureDesignTimeServices(services); var generator = services .BuildServiceProvider() .GetRequiredService <IModelCodeGenerator>(); var scaffoldedModel = generator.GenerateModel( model, "TestNamespace", "TestNamespace", "TestNamespace", /*contextDir:*/ string.Empty, "TestDbContext", "Initial Catalog=TestDatabase", options); assertScaffold(scaffoldedModel); var build = new BuildSource { References = { BuildReference.ByName("Microsoft.EntityFrameworkCore"), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational"), BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer") }, Sources = new List <string>( new[] { scaffoldedModel.ContextFile.Code }.Concat( scaffoldedModel.AdditionalFiles.Select(f => f.Code))) }; var assembly = build.BuildInMemory(); var context = (DbContext)assembly.CreateInstance("TestNamespace.TestDbContext"); var compiledModel = context.Model; assertModel(compiledModel); }
private void Test( Action <ModelBuilder> buildModel, ModelCodeGenerationOptions options, Action <ScaffoldedModel> assertScaffold, Action <IModel> assertModel) { var modelBuilder = new ModelBuilder(SqlServerConventionSetBuilder.Build()); buildModel(modelBuilder); modelBuilder.GetInfrastructure().Metadata.Validate(); var model = modelBuilder.Model; var services = new ServiceCollection() .AddEntityFrameworkDesignTimeServices(); new SqlServerDesignTimeServices().ConfigureDesignTimeServices(services); var generator = services .BuildServiceProvider() .GetRequiredService <IModelCodeGenerator>(); var scaffoldedModel = generator.GenerateModel( model, "TestNamespace", /*contextDir:*/ string.Empty, "TestDbContext", "Initial Catalog=TestDatabase", options); assertScaffold(scaffoldedModel); var build = new BuildSource { References = { BuildReference.ByName("Microsoft.EntityFrameworkCore"), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational"), BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer") }, Sources = new List <string>( Enumerable.Concat( new[] { scaffoldedModel.ContextFile.Code }, scaffoldedModel.AdditionalFiles.Select(f => f.Code))) }; var assembly = build.BuildInMemory(); var context = (DbContext)assembly.CreateInstance("TestNamespace.TestDbContext"); var compiledModel = context.Model; assertModel(compiledModel); }
protected virtual void AssertCompile(FileSet fileSet) { var fileContents = fileSet.Files.Select(fileSet.Contents).ToList(); var source = new BuildSource { Sources = fileContents }; foreach (var r in References) { source.References.Add(r); } source.BuildInMemory(); }
private Assembly Compile(string assemblyCode) { var build = new BuildSource { References = { BuildReference.ByName("Microsoft.EntityFrameworkCore"), BuildReference.ByName("Microsoft.EntityFrameworkCore.Design.Tests"), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational"), BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer"), BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite"), BuildReference.ByName("Microsoft.Extensions.DependencyInjection.Abstractions") }, Sources = { { "Startup.cs", assemblyCode } } }; return(build.BuildInMemory()); }
private ModelSnapshot CompileModelSnapshot(string modelSnapshotCode, string modelSnapshotTypeName) { var build = new BuildSource { References = { BuildReference.ByName("Microsoft.EntityFrameworkCore.Design.Tests"), BuildReference.ByName("Microsoft.EntityFrameworkCore"), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational") }, Sources = { modelSnapshotCode } }; var assembly = build.BuildInMemory(); var snapshotType = assembly.GetType(modelSnapshotTypeName, throwOnError: true, ignoreCase: false); var contextTypeAttribute = snapshotType.GetTypeInfo().GetCustomAttribute <DbContextAttribute>(); Assert.NotNull(contextTypeAttribute); Assert.Equal(typeof(MyContext), contextTypeAttribute.ContextType); return((ModelSnapshot)Activator.CreateInstance(snapshotType)); }
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()); }
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); }
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()); }
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()); }
protected void Test( Action <ModelBuilder> buildModel, ModelCodeGenerationOptions options, Action <ScaffoldedModel> assertScaffold, Action <IModel> assertModel, bool skipBuild = false) { var designServices = new ServiceCollection(); AddModelServices(designServices); var modelBuilder = SqlServerTestHelpers.Instance.CreateConventionBuilder(customServices: designServices); modelBuilder.Model.RemoveAnnotation(CoreAnnotationNames.ProductVersion); buildModel(modelBuilder); var model = modelBuilder.FinalizeModel(designTime: true, skipValidation: true); var services = CreateServices(); AddScaffoldingServices(services); var generator = services.BuildServiceProvider(validateScopes: true) .GetRequiredService <IModelCodeGenerator>(); options.ModelNamespace ??= "TestNamespace"; options.ContextName = "TestDbContext"; options.ConnectionString = "Initial Catalog=TestDatabase"; var scaffoldedModel = generator.GenerateModel( model, options); assertScaffold(scaffoldedModel); var build = new BuildSource { References = { BuildReference.ByName("Microsoft.EntityFrameworkCore.Abstractions"), BuildReference.ByName("Microsoft.EntityFrameworkCore"), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational"), BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer") }, Sources = new[] { scaffoldedModel.ContextFile }.Concat(scaffoldedModel.AdditionalFiles) .ToDictionary(f => f.Path, f => f.Code), NullableReferenceTypes = options.UseNullableReferenceTypes }; if (!skipBuild) { var assembly = build.BuildInMemory(); if (assertModel != null) { var contextNamespace = options.ContextNamespace ?? options.ModelNamespace; var context = (DbContext)assembly.CreateInstance( !string.IsNullOrEmpty(contextNamespace) ? contextNamespace + "." + options.ContextName : options.ContextName); var compiledModel = context.GetService <IDesignTimeModel>().Model; assertModel(compiledModel); } } }
public void Migrations_compile() { var codeHelper = new CSharpHelper(); var generator = new CSharpMigrationGenerator( codeHelper, new CSharpMigrationOperationGenerator(codeHelper), new CSharpModelGenerator(codeHelper)); var migrationCode = generator.Generate( "MyNamespace", "MyMigration", new MigrationOperation[0], new MigrationOperation[0]); Assert.Equal( @"using System.Collections.Generic; using Microsoft.Data.Entity.Relational.Migrations; using Microsoft.Data.Entity.Relational.Migrations.Builders; using Microsoft.Data.Entity.Relational.Migrations.Operations; namespace MyNamespace { public partial class MyMigration : Migration { public override void Up(MigrationBuilder migration) { } public override void Down(MigrationBuilder migration) { } } } ", migrationCode); var migrationMetadataCode = generator.GenerateMetadata( "MyNamespace", typeof(MyContext), "MyMigration", "20150511161616_MyMigration", "7.0.0", new Model()); Assert.Equal( @"using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Relational.Migrations.Infrastructure; using Microsoft.Data.Entity.Commands.Migrations; namespace MyNamespace { [ContextType(typeof(CodeCompilationTest.MyContext))] partial class MyMigration { public override string Id { get { return ""20150511161616_MyMigration""; } } public override string ProductVersion { get { return ""7.0.0""; } } public override void BuildTargetModel(ModelBuilder builder) { } } } ", migrationMetadataCode); var build = new BuildSource { References = { BuildReference.ByName(typeof(CodeCompilationTest).Assembly.GetName().Name), BuildReference.ByName("System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), BuildReference.ByName("EntityFramework.Core"), BuildReference.ByName("EntityFramework.Relational") }, Sources = { migrationCode, migrationMetadataCode } }; var assembly = build.BuildInMemory(); var migrationType = assembly.GetType("MyNamespace.MyMigration", throwOnError: true); var contextTypeAttribute = migrationType.GetCustomAttribute <ContextTypeAttribute>(); Assert.NotNull(contextTypeAttribute); Assert.Equal(typeof(MyContext), contextTypeAttribute.ContextType); var migration = (Migration)Activator.CreateInstance(migrationType); Assert.Equal("20150511161616_MyMigration", migration.Id); Assert.Equal("7.0.0", migration.ProductVersion); var migrationBuilder = new MigrationBuilder(); migration.Up(migrationBuilder); Assert.Empty(migrationBuilder.Operations); migrationBuilder = new MigrationBuilder(); migration.Down(migrationBuilder); Assert.Empty(migrationBuilder.Operations); var conventions = new ConventionSet(); var modelBuilder = new ModelBuilder(conventions); migration.BuildTargetModel(modelBuilder); Assert.Empty(modelBuilder.Model.EntityTypes); }
protected void Test(Action <ModelBuilder> buildModel, string expectedCode, Action <IModel> assert) { var modelBuilder = CreateConventionalModelBuilder(); modelBuilder.HasChangeTrackingStrategy(ChangeTrackingStrategy.Snapshot); buildModel(modelBuilder); modelBuilder.GetInfrastructure().Metadata.Validate(); CreateModelValidator().Validate(modelBuilder.Model); var model = modelBuilder.Model; var generator = new CSharpSnapshotGenerator(new CSharpSnapshotGeneratorDependencies(new CSharpHelper())); var builder = new IndentedStringBuilder(); generator.Generate("builder", model, builder); var code = builder.ToString(); 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); }