public override ConventionSet AddConventions(ConventionSet conventionSet) { Check.NotNull(conventionSet, nameof(conventionSet)); base.AddConventions(conventionSet); conventionSet.ModelInitializedConventions.Add(new SqlServerIdentityStrategyConvention()); return conventionSet; }
public virtual IModel Create(Action<ModelBuilder> onModelCreating) { Check.NotNull(onModelCreating, nameof(onModelCreating)); var model = new Model(); var conventions = new ConventionSet(); var modelBuilder = new ModelBuilder(conventions, model); onModelCreating(modelBuilder); return model; }
public void OnBaseEntityTypeSet_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); InternalEntityTypeBuilder entityTypeBuilder = null; var convention = new Mock<IBaseTypeConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<EntityType>())) .Returns<InternalEntityTypeBuilder, EntityType>((b, t) => { Assert.NotNull(b); Assert.Null(t); entityTypeBuilder = b; return true; }); conventions.BaseEntityTypeSetConventions.Add(convention.Object); var nullConvention = new Mock<IBaseTypeConvention>(); nullConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<EntityType>())) .Returns<InternalEntityTypeBuilder, EntityType>((b, t) => { Assert.Null(t); Assert.Same(entityTypeBuilder, b); return false; }); conventions.BaseEntityTypeSetConventions.Add(nullConvention.Object); var extraConvention = new Mock<IBaseTypeConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<EntityType>())) .Returns<InternalEntityTypeBuilder, EntityType>((b, t) => { Assert.False(true); return false; }); conventions.BaseEntityTypeSetConventions.Add(extraConvention.Object); var builder = new InternalModelBuilder(new Model(), conventions).Entity(typeof(SpecialOrder), ConfigurationSource.Convention); Assert.NotNull(builder.BaseType(typeof(Order), ConfigurationSource.Convention)); Assert.NotNull(entityTypeBuilder); }
public void OnPropertyAdded_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); InternalPropertyBuilder propertyBuilder = null; var convention = new Mock<IPropertyConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalPropertyBuilder>())).Returns<InternalPropertyBuilder>(b => { Assert.NotNull(b); propertyBuilder = new InternalPropertyBuilder(b.Metadata, b.ModelBuilder, ConfigurationSource.Convention); return propertyBuilder; }); conventions.PropertyAddedConventions.Add(convention.Object); var nullConvention = new Mock<IPropertyConvention>(); nullConvention.Setup(c => c.Apply(It.IsAny<InternalPropertyBuilder>())).Returns<InternalPropertyBuilder>(b => { Assert.Same(propertyBuilder, b); return null; }); conventions.PropertyAddedConventions.Add(nullConvention.Object); var extraConvention = new Mock<IPropertyConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalPropertyBuilder>())).Returns<InternalPropertyBuilder>(b => { Assert.False(true); return null; }); conventions.PropertyAddedConventions.Add(extraConvention.Object); var builder = new InternalModelBuilder(new Model(), conventions); var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention); var explicitKeyBuilder = entityBuilder.Property(typeof(int), "OrderId", ConfigurationSource.Convention); Assert.Null(explicitKeyBuilder); Assert.NotNull(propertyBuilder); }
public void OnEntityTypeAdded_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); InternalEntityTypeBuilder entityTypeBuilder = null; var convention = new Mock<IEntityTypeConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>())).Returns<InternalEntityTypeBuilder>(b => { Assert.NotNull(b); entityTypeBuilder = new InternalEntityTypeBuilder(b.Metadata, b.ModelBuilder); return entityTypeBuilder; }); conventions.EntityTypeAddedConventions.Add(convention.Object); var nullConvention = new Mock<IEntityTypeConvention>(); nullConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>())).Returns<InternalEntityTypeBuilder>(b => { Assert.Same(entityTypeBuilder, b); return null; }); conventions.EntityTypeAddedConventions.Add(nullConvention.Object); var extraConvention = new Mock<IEntityTypeConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>())).Returns<InternalEntityTypeBuilder>(b => { Assert.False(true); return null; }); conventions.EntityTypeAddedConventions.Add(extraConvention.Object); var builder = new InternalModelBuilder(new Model(), conventions); Assert.Null(builder.Entity(typeof(Order), ConfigurationSource.Convention)); Assert.NotNull(entityTypeBuilder); }
private static InternalModelBuilder CreateInternalModelBuilder() { var conventions = new ConventionSet(); conventions.EntityTypeAddedConventions.Add(new PropertyDiscoveryConvention()); conventions.EntityTypeAddedConventions.Add(new KeyDiscoveryConvention()); var keyConvention = new KeyConvention(); conventions.KeyAddedConventions.Add(keyConvention); conventions.ForeignKeyRemovedConventions.Add(keyConvention); return new InternalModelBuilder(new Model(), conventions); }
public void OnEntityTypeMemberIgnored_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); InternalEntityTypeBuilder entityTypeBuilder = null; var convention = new Mock<IEntityTypeMemberIgnoredConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<string>())) .Returns<InternalEntityTypeBuilder, string>((b, t) => { Assert.NotNull(b); Assert.Equal("A", t); entityTypeBuilder = b; return true; }); conventions.EntityTypeMemberIgnoredConventions.Add(convention.Object); var nullConvention = new Mock<IEntityTypeMemberIgnoredConvention>(); nullConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<string>())) .Returns<InternalEntityTypeBuilder, string>((b, t) => { Assert.Equal("A", t); Assert.Same(entityTypeBuilder, b); return false; }); conventions.EntityTypeMemberIgnoredConventions.Add(nullConvention.Object); var extraConvention = new Mock<IEntityTypeMemberIgnoredConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<string>())) .Returns<InternalEntityTypeBuilder, string>((b, t) => { Assert.False(true); return false; }); conventions.EntityTypeMemberIgnoredConventions.Add(extraConvention.Object); var builder = new InternalModelBuilder(new Model(), conventions).Entity(typeof(SpecialOrder), ConfigurationSource.Convention); Assert.NotNull(builder.Ignore("A", ConfigurationSource.Convention)); Assert.NotNull(entityTypeBuilder); }
public void ValidatingModel_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); var nullConventionCalled = false; InternalModelBuilder modelBuilder = null; var convention = new Mock<IModelConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalModelBuilder>())).Returns<InternalModelBuilder>(b => { Assert.NotNull(b); modelBuilder = new InternalModelBuilder(b.Metadata, new ConventionSet()); return b; }); conventions.ModelBuiltConventions.Add(convention.Object); var nullConvention = new Mock<IModelConvention>(); nullConvention.Setup(c => c.Apply(It.IsAny<InternalModelBuilder>())).Returns<InternalModelBuilder>(b => { nullConventionCalled = true; return null; }); conventions.ModelBuiltConventions.Add(nullConvention.Object); var extraConvention = new Mock<IModelConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalModelBuilder>())).Returns<InternalModelBuilder>(b => { Assert.False(true); return null; }); conventions.ModelBuiltConventions.Add(extraConvention.Object); var builder = new ModelBuilder(conventions).Validate(); Assert.NotNull(builder); Assert.True(nullConventionCalled); Assert.NotNull(modelBuilder); }
public void OnNavigationAdded_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); InternalRelationshipBuilder relationshipBuilder = null; var orderIgnored = false; var orderDetailsIgnored = false; var convention = new Mock<INavigationConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>(), It.IsAny<Navigation>())).Returns((InternalRelationshipBuilder b, Navigation n) => { Assert.NotNull(b); relationshipBuilder = new InternalRelationshipBuilder(b.Metadata, b.ModelBuilder, ConfigurationSource.Convention); return relationshipBuilder; }); conventions.NavigationAddedConventions.Add(convention.Object); var nullConvention = new Mock<INavigationConvention>(); nullConvention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>(), It.IsAny<Navigation>())).Returns((InternalRelationshipBuilder b, Navigation n) => { Assert.Same(relationshipBuilder, b); if (n.Name == "Order") { orderIgnored = true; } if (n.Name == "OrderDetails") { orderDetailsIgnored = true; } return null; }); conventions.NavigationAddedConventions.Add(nullConvention.Object); var extraConvention = new Mock<INavigationConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>(), It.IsAny<Navigation>())).Returns((InternalRelationshipBuilder b, Navigation n) => { Assert.False(true); return null; }); conventions.NavigationAddedConventions.Add(extraConvention.Object); var builder = new InternalModelBuilder(new Model(), conventions); var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention); entityBuilder.PrimaryKey(new[] { "OrderId" }, ConfigurationSource.Convention); Assert.Null(entityBuilder.Relationship(typeof(Order), typeof(OrderDetails), "Order", "OrderDetails", ConfigurationSource.Convention, isUnique: true)); Assert.True(orderIgnored); Assert.False(orderDetailsIgnored); Assert.NotNull(relationshipBuilder); }
public void OnForeignKeyRemoved_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); var foreignKeyRemoved = false; var convention = new Mock<IForeignKeyRemovedConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<ForeignKey>())).Callback(() => foreignKeyRemoved = true); conventions.ForeignKeyRemovedConventions.Add(convention.Object); var builder = new InternalModelBuilder(new Model(), conventions); var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention); var foreignKey = new ForeignKey( new[] { entityBuilder.Property("FK", typeof(int), ConfigurationSource.Convention).Metadata }, entityBuilder.HasKey(new[] { "OrderId" }, ConfigurationSource.Convention).Metadata, entityBuilder.Metadata, entityBuilder.Metadata); var conventionDispatcher = new ConventionDispatcher(conventions); conventionDispatcher.OnForeignKeyRemoved(entityBuilder, foreignKey); Assert.True(foreignKeyRemoved); }
public void OnPrimaryKeySet_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); InternalKeyBuilder internalKeyBuilder = null; var convention = new Mock<IPrimaryKeyConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalKeyBuilder>(), It.IsAny<Key>())) .Returns<InternalKeyBuilder, Key>((b, t) => { Assert.NotNull(b); Assert.Null(t); internalKeyBuilder = b; return true; }); conventions.PrimaryKeySetConventions.Add(convention.Object); var nullConvention = new Mock<IPrimaryKeyConvention>(); nullConvention.Setup(c => c.Apply(It.IsAny<InternalKeyBuilder>(), It.IsAny<Key>())) .Returns<InternalKeyBuilder, Key>((b, t) => { Assert.Null(t); Assert.Same(internalKeyBuilder, b); return false; }); conventions.PrimaryKeySetConventions.Add(nullConvention.Object); var extraConvention = new Mock<IPrimaryKeyConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalKeyBuilder>(), It.IsAny<Key>())) .Returns<InternalKeyBuilder, Key>((b, t) => { Assert.False(true); return false; }); conventions.PrimaryKeySetConventions.Add(extraConvention.Object); var entityBuilder = new InternalModelBuilder(new Model(), conventions) .Entity(typeof(Order), ConfigurationSource.Convention); entityBuilder.HasKey(new[] { "OrderId" }, ConfigurationSource.Convention); Assert.NotNull(entityBuilder.PrimaryKey(new[] { "OrderId" }, ConfigurationSource.Convention)); Assert.NotNull(internalKeyBuilder); }
public void OnForeignKeyAdded_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); InternalRelationshipBuilder relationshipBuilder = null; var convention = new Mock<IForeignKeyConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>())).Returns<InternalRelationshipBuilder>(b => { Assert.NotNull(b); relationshipBuilder = new InternalRelationshipBuilder(b.Metadata, b.ModelBuilder, null); return relationshipBuilder; }); conventions.ForeignKeyAddedConventions.Add(convention.Object); var nullConvention = new Mock<IForeignKeyConvention>(); nullConvention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>())).Returns<InternalRelationshipBuilder>(b => { Assert.Same(relationshipBuilder, b); return null; }); conventions.ForeignKeyAddedConventions.Add(nullConvention.Object); var extraConvention = new Mock<IForeignKeyConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>())).Returns<InternalRelationshipBuilder>(b => { Assert.False(true); return null; }); conventions.ForeignKeyAddedConventions.Add(extraConvention.Object); var builder = new InternalModelBuilder(new Model(), conventions); var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention); entityBuilder.PrimaryKey(new[] { "OrderId" }, ConfigurationSource.Convention); Assert.Null(entityBuilder.Relationship(typeof(Order), typeof(Order), null, null, ConfigurationSource.Convention)); Assert.NotNull(relationshipBuilder); }
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 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.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); }
public void OnPropertyNullableChanged_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); var convention1 = new PropertyNullableConvention(false); var convention2 = new PropertyNullableConvention(true); var convention3 = new PropertyNullableConvention(false); conventions.PropertyNullableChangedConventions.Add(convention1); conventions.PropertyNullableChangedConventions.Add(convention2); conventions.PropertyNullableChangedConventions.Add(convention3); var builder = new ModelBuilder(conventions); builder.Entity<Order>().Property(e => e.Name).IsRequired(); Assert.Equal(new bool?[] { false }, convention1.Calls); Assert.Equal(new bool?[] { false }, convention2.Calls); Assert.Empty(convention3.Calls); builder.Entity<Order>().Property(e => e.Name).IsRequired(false); Assert.Equal(new bool?[] { false, true }, convention1.Calls); Assert.Equal(new bool?[] { false, true }, convention2.Calls); Assert.Empty(convention3.Calls); builder.Entity<Order>().Property(e => e.Name).IsRequired(false); Assert.Equal(new bool?[] { false, true }, convention1.Calls); Assert.Equal(new bool?[] { false, true }, convention2.Calls); Assert.Empty(convention3.Calls); builder.Entity<Order>().Property(e => e.Name).IsRequired(); Assert.Equal(new bool?[] { false, true, false }, convention1.Calls); Assert.Equal(new bool?[] { false, true, false }, convention2.Calls); Assert.Empty(convention3.Calls); }
public void OnNavigationRemoved_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); InternalEntityTypeBuilder dependentEntityTypeBuilderFromConvention = null; InternalEntityTypeBuilder principalEntityBuilderFromConvention = null; var convention = new Mock<INavigationRemovedConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<string>())).Returns((InternalEntityTypeBuilder s, InternalEntityTypeBuilder t, string n) => { dependentEntityTypeBuilderFromConvention = s; principalEntityBuilderFromConvention = t; Assert.Equal(nameof(OrderDetails.Order), n); return false; }); conventions.NavigationRemovedConventions.Add(convention.Object); var extraConvention = new Mock<INavigationRemovedConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<string>())).Returns((InternalEntityTypeBuilder s, InternalEntityTypeBuilder t, string n) => { Assert.False(true); return false; }); conventions.NavigationRemovedConventions.Add(extraConvention.Object); var builder = new InternalModelBuilder(new Model(), conventions); var principalEntityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention); var dependentEntityBuilder = builder.Entity(typeof(OrderDetails), ConfigurationSource.Convention); var relationshipBuilder = dependentEntityBuilder.Relationship(principalEntityBuilder, nameof(OrderDetails.Order), nameof(Order.OrderDetails), ConfigurationSource.Convention); relationshipBuilder.DependentToPrincipal(null, ConfigurationSource.Convention); Assert.NotNull(relationshipBuilder); Assert.Same(dependentEntityTypeBuilderFromConvention, dependentEntityBuilder); Assert.Same(principalEntityBuilderFromConvention, principalEntityBuilder); }