public void GetContextType_works_with_multiple_assemblies() { using (var directory = new TempDirectory()) { var targetDir = directory.Path; var contextsSource = new BuildSource { TargetDir = targetDir, References = { BuildReference.ByName("EntityFramework.Core", copyLocal: true), BuildReference.ByName("EntityFramework.Commands", copyLocal: true) }, Sources = { @" using Microsoft.Data.Entity; namespace MyProject { public class Context1 : DbContext { } public class Context2 : DbContext { } }" } }; var contextsBuild = contextsSource.Build(); var migrationsSource = new BuildSource { TargetDir = targetDir, References = { BuildReference.ByName("System.Collections.Immutable", copyLocal: true), BuildReference.ByName("System.Reflection.Metadata", copyLocal: true), BuildReference.ByName("EntityFramework.Core"), BuildReference.ByName("EntityFramework.Relational", copyLocal: true), BuildReference.ByName("EntityFramework.Relational.Design", copyLocal: true), BuildReference.ByName("Microsoft.CodeAnalysis", copyLocal: true), BuildReference.ByName("Microsoft.Framework.Logging", copyLocal: true), BuildReference.ByName("Microsoft.Framework.Logging.Abstractions", copyLocal: true), BuildReference.ByPath(contextsBuild.TargetPath) }, Sources = { @" using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; using Microsoft.Data.Entity.Migrations; namespace MyProject { internal class Context3 : DbContext { } namespace Migrations { namespace Context1Migrations { [DbContext(typeof(Context1))] [Migration(""000000000000000_Context1Migration"")] public class Context1Migration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } } } namespace Context2Migrations { [DbContext(typeof(Context2))] [Migration(""000000000000000_Context2Migration"")] public class Context2Migration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } } } } }" } }; var migrationsBuild = migrationsSource.Build(); using (var executor = new OperationExecutorWrapper(targetDir, migrationsBuild.TargetName, targetDir, "MyProject")) { var contextTypes = executor.GetContextTypes(); Assert.Equal(3, contextTypes.Count()); } } }
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.Migrations; using Microsoft.Data.Entity.Migrations.Builders; using Microsoft.Data.Entity.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.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); }
public void GetMigrations_filters_by_context_name() { using (var directory = new TempDirectory()) { var targetDir = directory.Path; var source = new BuildSource { TargetDir = targetDir, References = { BuildReference.ByName("System.Collections.Immutable", copyLocal: true), BuildReference.ByName("System.Interactive.Async", copyLocal: true), BuildReference.ByName("System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"), BuildReference.ByName("EntityFramework.Core", copyLocal: true), BuildReference.ByName("EntityFramework.Commands", copyLocal: true), BuildReference.ByName("EntityFramework.Relational", copyLocal: true), BuildReference.ByName("EntityFramework.Relational.Design", copyLocal: true), BuildReference.ByName("EntityFramework.SqlServer", copyLocal: true), BuildReference.ByName("Microsoft.CodeAnalysis", copyLocal: true), BuildReference.ByName("Microsoft.Framework.Caching.Abstractions", copyLocal: true), BuildReference.ByName("Microsoft.Framework.Caching.Memory", copyLocal: true), BuildReference.ByName("Microsoft.Framework.DependencyInjection", copyLocal: true), BuildReference.ByName("Microsoft.Framework.DependencyInjection.Abstractions", copyLocal: true), BuildReference.ByName("Microsoft.Framework.Logging", copyLocal: true), BuildReference.ByName("Microsoft.Framework.Logging.Abstractions", copyLocal: true), BuildReference.ByName("Microsoft.Framework.OptionsModel", copyLocal: true), BuildReference.ByName("Remotion.Linq", copyLocal: true) }, Sources = { @" using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; using Microsoft.Data.Entity.Migrations; namespace MyProject { internal class Context1 : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(""Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SimpleProject.SimpleContext;Integrated Security=True""); } } internal class Context2 : DbContext { } namespace Migrations { namespace Context1Migrations { [DbContext(typeof(Context1))] [Migration(""000000000000000_Context1Migration"")] public class Context1Migration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } } } namespace Context2Migrations { [DbContext(typeof(Context2))] [Migration(""000000000000000_Context2Migration"")] public class Context2Migration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } } } } }" } }; var build = source.Build(); using (var executor = new OperationExecutorWrapper(targetDir, build.TargetName, targetDir, "MyProject")) { var migrations = executor.GetMigrations("Context1"); Assert.Equal(1, migrations.Count()); } } }
public void GetMigrations_filters_by_context_name() { using (var directory = new TempDirectory()) { var targetDir = directory.Path; var source = new BuildSource { TargetDir = targetDir, References = { BuildReference.ByName("System.Interactive.Async", true), BuildReference.ByName("Microsoft.EntityFrameworkCore", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.Abstractions", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.Design", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer", true), BuildReference.ByName("Microsoft.Extensions.Caching.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.Caching.Memory", true), BuildReference.ByName("Microsoft.Extensions.Configuration.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.DependencyInjection", true), BuildReference.ByName("Microsoft.Extensions.DependencyInjection.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.Logging", true), BuildReference.ByName("Microsoft.Extensions.Logging.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.Options", true), BuildReference.ByName("Microsoft.Extensions.Primitives", true), BuildReference.ByName("Remotion.Linq", true) }, Sources = { @" using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; namespace MyProject { internal class Context1 : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(""Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SimpleProject.SimpleContext;Integrated Security=True""); } } internal class Context2 : DbContext { } namespace Migrations { namespace Context1Migrations { [DbContext(typeof(Context1))] [Migration(""000000000000000_Context1Migration"")] public class Context1Migration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } } } namespace Context2Migrations { [DbContext(typeof(Context2))] [Migration(""000000000000000_Context2Migration"")] public class Context2Migration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } } } } }" } }; var build = source.Build(); using (var executor = CreateExecutorFromBuildResult(build, "MyProject", "C#")) { try { var migrations = executor.GetMigrations("Context1"); Assert.Single(migrations); } catch (WrappedException ex) { throw new WrappedXunitException(ex); } } } }
public void AddMigration_begins_new_namespace_when_foreign_migrations() { using (var directory = new TempDirectory()) { var targetDir = directory.Path; var source = new BuildSource { TargetDir = targetDir, References = { BuildReference.ByName("System.Interactive.Async", true), BuildReference.ByName("Microsoft.EntityFrameworkCore", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.Abstractions", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.Design", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer", true), BuildReference.ByName("Microsoft.Extensions.Caching.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.Caching.Memory", true), BuildReference.ByName("Microsoft.Extensions.Configuration.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.DependencyInjection", true), BuildReference.ByName("Microsoft.Extensions.DependencyInjection.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.Logging", true), BuildReference.ByName("Microsoft.Extensions.Logging.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.Options", true), BuildReference.ByName("Microsoft.Extensions.Primitives", true), BuildReference.ByName("Remotion.Linq", true) }, Sources = { @" using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; namespace MyProject { internal class MyFirstContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(""Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyProject.MyFirstContext""); } } internal class MySecondContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(""Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyProject.MySecondContext""); } } namespace Migrations { [DbContext(typeof(MyFirstContext))] [Migration(""20151006140723_InitialCreate"")] public class InitialCreate : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } } } }" } }; var build = source.Build(); using (var executor = CreateExecutorFromBuildResult(build, "MyProject", "C#")) { try { var artifacts = executor.AddMigration("MyMigration", /*outputDir:*/ null, "MySecondContext"); Assert.Equal(3, artifacts.Keys.Count); } catch (WrappedException ex) { throw new WrappedXunitException(ex); } Assert.True(Directory.Exists(Path.Combine(targetDir, @"Migrations\MySecond"))); } } }
public void GetContextType_works_with_multiple_assemblies() { using (var directory = new TempDirectory()) { var targetDir = directory.Path; var contextsSource = new BuildSource { TargetDir = targetDir, References = { BuildReference.ByName("Microsoft.EntityFrameworkCore", true) }, Sources = { @" using Microsoft.EntityFrameworkCore; namespace MyProject { public class Context1 : DbContext { } public class Context2 : DbContext { } }" } }; var contextsBuild = contextsSource.Build(); var migrationsSource = new BuildSource { TargetDir = targetDir, References = { BuildReference.ByName("Microsoft.EntityFrameworkCore"), BuildReference.ByName("Microsoft.EntityFrameworkCore.Abstractions", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.Design", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational", true), BuildReference.ByName("Microsoft.Extensions.DependencyInjection", true), BuildReference.ByName("Microsoft.Extensions.DependencyInjection.Abstractions",true), BuildReference.ByName("Microsoft.Extensions.Logging", true), BuildReference.ByName("Microsoft.Extensions.Logging.Abstractions", true), BuildReference.ByPath(contextsBuild.TargetPath) }, Sources = { @" using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; namespace MyProject { internal class Context3 : DbContext { } namespace Migrations { namespace Context1Migrations { [DbContext(typeof(Context1))] [Migration(""000000000000000_Context1Migration"")] public class Context1Migration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } } } namespace Context2Migrations { [DbContext(typeof(Context2))] [Migration(""000000000000000_Context2Migration"")] public class Context2Migration : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } } } } }" } }; var build = migrationsSource.Build(); using (var executor = CreateExecutorFromBuildResult(build, "MyProject", "C#")) { try { var contextTypes = executor.GetContextTypes(); Assert.Equal(3, contextTypes.Count()); } catch (WrappedException ex) { throw new WrappedXunitException(ex); } } } }
public SimpleProject() { var source = new BuildSource { TargetDir = TargetDir, References = { BuildReference.ByName("System.Diagnostics.DiagnosticSource", true), BuildReference.ByName("System.Interactive.Async", true), BuildReference.ByName("System.Data.SqlClient", true), BuildReference.ByName("Microsoft.EntityFrameworkCore", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.Design", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.Relational", true), BuildReference.ByName("Microsoft.EntityFrameworkCore.SqlServer", true), BuildReference.ByName("Microsoft.Extensions.Caching.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.Caching.Memory", true), BuildReference.ByName("Microsoft.Extensions.Configuration.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.DependencyInjection", true), BuildReference.ByName("Microsoft.Extensions.DependencyInjection.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.Logging", true), BuildReference.ByName("Microsoft.Extensions.Logging.Abstractions", true), BuildReference.ByName("Microsoft.Extensions.Options", true), BuildReference.ByName("Microsoft.Extensions.Primitives", true), BuildReference.ByName("Remotion.Linq", true) }, Sources = { @" using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; namespace SimpleProject { internal class SimpleContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(""Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SimpleProject.SimpleContext;Integrated Security=True""); } } namespace Migrations { [DbContext(typeof(SimpleContext))] [Migration(""20141010222726_InitialCreate"")] public class InitialCreate : Migration { protected override void Up(MigrationBuilder migrationBuilder) { } } } }" } }; var build = source.Build(); File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, build.TargetPath + ".config"); Executor = new AppDomainOperationExecutor( build.TargetPath, build.TargetPath, build.TargetDir, build.TargetDir, "SimpleProject"); }