public void Snapshot_with_default_values_are_round_tripped() { var codeHelper = new CSharpHelper(); var generator = new CSharpMigrationsGenerator( new MigrationsCodeGeneratorDependencies(), new CSharpMigrationsGeneratorDependencies( codeHelper, new CSharpMigrationOperationGenerator( new CSharpMigrationOperationGeneratorDependencies(codeHelper)), new CSharpSnapshotGenerator(new CSharpSnapshotGeneratorDependencies(codeHelper)))); var modelBuilder = new ModelBuilder(TestServiceFactory.Instance.Create <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); } }
public void Migrations_compile() { var codeHelper = new CSharpHelper(); var generator = new CSharpMigrationsGenerator( new MigrationsCodeGeneratorDependencies(), new CSharpMigrationsGeneratorDependencies( codeHelper, new CSharpMigrationOperationGenerator( new CSharpMigrationOperationGeneratorDependencies(codeHelper)), new CSharpSnapshotGenerator(new CSharpSnapshotGeneratorDependencies(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 System.Text.RegularExpressions; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage; 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, ignoreLineEndingDifferences: true); var migrationMetadataCode = generator.GenerateMetadata( "MyNamespace", typeof(MyContext), "MyMigration", "20150511161616_MyMigration", new Model { ["Some:EnumValue"] = RegexOptions.Multiline, ["Relational:DbFunction:MyFunc"] = new object() }); Assert.Equal( @"// <auto-generated /> using System; using System.Text.RegularExpressions; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations.Design; namespace MyNamespace { [DbContext(typeof(CSharpMigrationsGeneratorTest.MyContext))] [Migration(""20150511161616_MyMigration"")] partial class MyMigration { protected override void BuildTargetModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder .HasAnnotation(""Some:EnumValue"", RegexOptions.Multiline); #pragma warning restore 612, 618 } } } ", migrationMetadataCode, ignoreLineEndingDifferences: true); var build = new BuildSource { References = { 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 Snapshots_compile() { var codeHelper = new CSharpHelper(); var generator = new CSharpMigrationsGenerator( new MigrationsCodeGeneratorDependencies(), new CSharpMigrationsGeneratorDependencies( codeHelper, new CSharpMigrationOperationGenerator( new CSharpMigrationOperationGeneratorDependencies(codeHelper)), new CSharpSnapshotGenerator(new CSharpSnapshotGeneratorDependencies(codeHelper)))); var model = new Model { ["Some:EnumValue"] = RegexOptions.Multiline, ["Relational:DbFunction:MyFunc"] = new object() }; var entityType = model.AddEntityType("Cheese"); entityType.AddProperty("Pickle", typeof(StringBuilder)); var modelSnapshotCode = generator.GenerateSnapshot( "MyNamespace", typeof(MyContext), "MySnapshot", model); Assert.Equal( @"// <auto-generated /> using System; using System.Text; using System.Text.RegularExpressions; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations.Design; namespace MyNamespace { [DbContext(typeof(CSharpMigrationsGeneratorTest.MyContext))] partial class MySnapshot : ModelSnapshot { protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder .HasAnnotation(""Some:EnumValue"", RegexOptions.Multiline); modelBuilder.Entity(""Cheese"", b => { b.Property<StringBuilder>(""Pickle""); b.ToTable(""Cheese""); }); #pragma warning restore 612, 618 } } } ", modelSnapshotCode, ignoreLineEndingDifferences: true); var snapshot = CompileModelSnapshot(modelSnapshotCode, "MyNamespace.MySnapshot"); Assert.Equal(1, snapshot.Model.GetEntityTypes().Count()); }
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.EntityFrameworkCore.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.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))] [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 NETCOREAPP1_0 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("Microsoft.EntityFrameworkCore.Tools.Core.FunctionalTests", depContextAssembly: GetType().GetTypeInfo().Assembly), 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(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.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 build = new BuildSource { References = { #if NETCOREAPP1_0 BuildReference.ByName("System.Runtime"), 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("Microsoft.EntityFrameworkCore.Tools.Core.FunctionalTests", depContextAssembly: GetType().GetTypeInfo().Assembly), BuildReference.ByName("Microsoft.EntityFrameworkCore"), BuildReference.ByName("Microsoft.EntityFrameworkCore.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()); }
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); } }
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()); }