public override ConventionSet AddConventions(ConventionSet conventionSet) { Check.NotNull(conventionSet, nameof(conventionSet)); base.AddConventions(conventionSet); conventionSet.ModelInitializedConventions.Add(new SqlServerValueGenerationStrategyConvention()); return conventionSet; }
public void OnBaseEntityTypeSet_calls_apply_on_conventions_in_order(bool useBuilder) { 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); if (useBuilder) { Assert.NotNull(builder.HasBaseType(typeof(Order), ConfigurationSource.Convention)); } else { builder.Metadata.HasBaseType(builder.Metadata.Model.AddEntityType(typeof(Order)), ConfigurationSource.Convention); } Assert.NotNull(entityTypeBuilder); }
public void OnEntityTypeAdded_calls_apply_on_conventions_in_order(bool useBuilder) { 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)); if (useBuilder) { Assert.Null(builder.Entity(typeof(Order), ConfigurationSource.Convention)); } else { Assert.Null(builder.Metadata.AddEntityType(typeof(Order), ConfigurationSource.Convention)); } Assert.NotNull(entityTypeBuilder); }
public void ValidatingModel_calls_apply_on_conventions_in_order(bool useBuilder) { 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); 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); Assert.Null(useBuilder ? new InternalModelBuilder(new Model(conventions)).Validate() : new Model(conventions).Validate()); Assert.True(nullConventionCalled); Assert.NotNull(modelBuilder); }
public void OnPrincipalKeySet_calls_apply_on_conventions_in_order(bool useBuilder) { var conventions = new ConventionSet(); InternalRelationshipBuilder relationshipBuilder = null; var convention = new Mock<IPrincipalEndConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>())).Returns<InternalRelationshipBuilder>(b => { Assert.NotNull(b); relationshipBuilder = new InternalRelationshipBuilder(b.Metadata, b.ModelBuilder); return relationshipBuilder; }); conventions.PrincipalEndSetConventions.Add(convention.Object); var nullConvention = new Mock<IPrincipalEndConvention>(); nullConvention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>())).Returns<InternalRelationshipBuilder>(b => { Assert.Same(relationshipBuilder, b); return null; }); conventions.PrincipalEndSetConventions.Add(nullConvention.Object); var extraConvention = new Mock<IPrincipalEndConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalRelationshipBuilder>())).Returns<InternalRelationshipBuilder>(b => { Assert.False(true); return null; }); conventions.PrincipalEndSetConventions.Add(extraConvention.Object); var modelBuilder = new InternalModelBuilder(new Model(conventions)); var entityBuilder = modelBuilder.Entity(typeof(Order), ConfigurationSource.Convention); entityBuilder.PrimaryKey(new[] { "OrderId" }, ConfigurationSource.Convention); var dependentEntityBuilder = modelBuilder.Entity(typeof(OrderDetails), ConfigurationSource.Convention); if (useBuilder) { Assert.Null( dependentEntityBuilder .Relationship(entityBuilder, ConfigurationSource.Convention) .HasPrincipalKey(entityBuilder.Metadata.FindPrimaryKey().Properties, ConfigurationSource.Convention)); } else { Assert.Null(dependentEntityBuilder.Metadata.AddForeignKey( dependentEntityBuilder.Property("Id", typeof(int), ConfigurationSource.Convention).Metadata, entityBuilder.Metadata.FindPrimaryKey(), entityBuilder.Metadata, ConfigurationSource.Convention)); } Assert.NotNull(relationshipBuilder); }
public void OnNavigationRemoved_calls_apply_on_conventions_in_order(bool useBuilder) { 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>(), It.IsAny<PropertyInfo>())) .Returns((InternalEntityTypeBuilder s, InternalEntityTypeBuilder t, string n, PropertyInfo p) => { dependentEntityTypeBuilderFromConvention = s; principalEntityBuilderFromConvention = t; Assert.Equal(nameof(OrderDetails.Order), n); Assert.Equal(nameof(OrderDetails.Order), p.Name); 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>(), It.IsAny<PropertyInfo>())) .Returns((InternalEntityTypeBuilder s, InternalEntityTypeBuilder t, string n, PropertyInfo p) => { 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); if (useBuilder) { Assert.NotNull(relationshipBuilder.DependentToPrincipal((string)null, ConfigurationSource.Convention)); } else { Assert.NotNull(relationshipBuilder.Metadata.HasDependentToPrincipal((string)null, ConfigurationSource.Convention)); } Assert.Same(dependentEntityTypeBuilderFromConvention, dependentEntityBuilder); Assert.Same(principalEntityBuilderFromConvention, principalEntityBuilder); }
public void OnNavigationAdded_calls_apply_on_conventions_in_order(bool useBuilder) { 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); 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 principalEntityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention); var dependentEntityBuilder = builder.Entity(typeof(OrderDetails), ConfigurationSource.Convention); if (useBuilder) { Assert.Null(dependentEntityBuilder.Relationship(principalEntityBuilder, OrderDetails.OrderProperty, Order.OrderDetailsProperty, ConfigurationSource.Convention)); } else { var fk = dependentEntityBuilder.Relationship(principalEntityBuilder, ConfigurationSource.Convention) .IsUnique(true, ConfigurationSource.Convention) .Metadata; Assert.Null(fk.HasDependentToPrincipal(OrderDetails.OrderProperty)); } Assert.True(orderIgnored); Assert.False(orderDetailsIgnored); Assert.NotNull(relationshipBuilder); }
/// <summary> /// <para> /// Call this method to build a <see cref="ModelBuilder" /> for SQL Server outside of <see cref="DbContext.OnModelCreating" />. /// </para> /// <para> /// Note that it is unusual to use this method. /// Consider using <see cref="DbContext" /> in the normal way instead. /// </para> /// </summary> /// <returns> The convention set. </returns> public static ModelBuilder CreateModelBuilder() { using var serviceScope = CreateServiceScope(); using var context = serviceScope.ServiceProvider.GetRequiredService <DbContext>(); return(new ModelBuilder(ConventionSet.CreateConventionSet(context), context.GetService <ModelDependencies>())); }
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); conventions.PrimaryKeySetConventions.Add(keyConvention); return new InternalModelBuilder(new Model(conventions)); }
public void OnKeyRemoved_calls_apply_on_conventions_in_order() { var conventions = new ConventionSet(); InternalKeyBuilder keyBuilder = null; var convention = new Mock<IKeyRemovedConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<Key>())) .Callback<InternalEntityTypeBuilder, Key>((b, k) => { Assert.NotNull(b); Assert.NotNull(k); keyBuilder = new InternalKeyBuilder(k, b.ModelBuilder); }); conventions.KeyRemovedConventions.Add(convention.Object); var extraConvention = new Mock<IKeyRemovedConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalEntityTypeBuilder>(), It.IsAny<Key>())) .Callback<InternalEntityTypeBuilder, Key>((b, k) => { Assert.NotNull(b); Assert.NotNull(k); Assert.NotNull(keyBuilder); }); conventions.KeyRemovedConventions.Add(extraConvention.Object); var builder = new InternalModelBuilder(new Model(conventions)); var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention); var key = entityBuilder.HasKey(new List<string> { "OrderId" }, ConfigurationSource.Convention).Metadata; Assert.Same(key, entityBuilder.Metadata.RemoveKey(key.Properties)); Assert.NotNull(keyBuilder); }
public void OnKeyAdded_calls_apply_on_conventions_in_order(bool useBuilder) { var conventions = new ConventionSet(); InternalKeyBuilder keyBuilder = null; var convention = new Mock<IKeyConvention>(); convention.Setup(c => c.Apply(It.IsAny<InternalKeyBuilder>())).Returns<InternalKeyBuilder>(b => { Assert.NotNull(b); keyBuilder = new InternalKeyBuilder(b.Metadata, b.ModelBuilder); return keyBuilder; }); conventions.KeyAddedConventions.Add(convention.Object); var nullConvention = new Mock<IKeyConvention>(); nullConvention.Setup(c => c.Apply(It.IsAny<InternalKeyBuilder>())).Returns<InternalKeyBuilder>(b => { Assert.Same(keyBuilder, b); return null; }); conventions.KeyAddedConventions.Add(nullConvention.Object); var extraConvention = new Mock<IKeyConvention>(); extraConvention.Setup(c => c.Apply(It.IsAny<InternalKeyBuilder>())).Returns<InternalKeyBuilder>(b => { Assert.False(true); return null; }); conventions.KeyAddedConventions.Add(extraConvention.Object); var builder = new InternalModelBuilder(new Model(conventions)); var entityBuilder = builder.Entity(typeof(Order), ConfigurationSource.Convention); if (useBuilder) { Assert.Null(entityBuilder.HasKey(new List<string> { "OrderId" }, ConfigurationSource.Convention)); } else { var property = entityBuilder.Property("OrderId", ConfigurationSource.Convention).Metadata; property.IsNullable = false; Assert.Null(entityBuilder.Metadata.AddKey(property)); } Assert.NotNull(keyBuilder); }
public void OnPropertyAdded_calls_apply_on_conventions_in_order_for_non_shadow_property(bool useBuilder) { 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); Assert.Equal("OrderId", b.Metadata.Name); propertyBuilder = new InternalPropertyBuilder(b.Metadata, b.ModelBuilder); 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); if (useBuilder) { Assert.Null(entityBuilder.Property(Order.OrderIdProperty, ConfigurationSource.Convention)); } else { Assert.Null(entityBuilder.Metadata.AddProperty(Order.OrderIdProperty)); } Assert.NotNull(propertyBuilder); }
public void OnEntityTypeMemberIgnored_calls_apply_on_conventions_in_order(bool useBuilder) { 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); if (useBuilder) { Assert.NotNull(builder.Ignore("A", ConfigurationSource.Convention)); } else { builder.Metadata.Ignore("A", ConfigurationSource.Convention); } Assert.NotNull(entityTypeBuilder); }
/// <summary> /// Builds and returns the convention set for the current database provider. /// </summary> /// <returns> The convention set for the current database provider. </returns> public override ConventionSet CreateConventionSet() { var conventionSet = base.CreateConventionSet(); var valueGenerationStrategyConvention = new SqlServerValueGenerationStrategyConvention(Dependencies, RelationalDependencies); conventionSet.ModelInitializedConventions.Add(valueGenerationStrategyConvention); conventionSet.ModelInitializedConventions.Add( new RelationalMaxIdentifierLengthConvention(128, Dependencies, RelationalDependencies)); ValueGenerationConvention valueGenerationConvention = new SqlServerValueGenerationConvention(Dependencies, RelationalDependencies); var sqlServerIndexConvention = new SqlServerIndexConvention(Dependencies, RelationalDependencies, _sqlGenerationHelper); ReplaceConvention(conventionSet.EntityTypeBaseTypeChangedConventions, valueGenerationConvention); conventionSet.EntityTypeBaseTypeChangedConventions.Add(sqlServerIndexConvention); var sqlServerInMemoryTablesConvention = new SqlServerMemoryOptimizedTablesConvention(Dependencies, RelationalDependencies); conventionSet.EntityTypeAnnotationChangedConventions.Add(sqlServerInMemoryTablesConvention); ReplaceConvention( conventionSet.EntityTypeAnnotationChangedConventions, (RelationalValueGenerationConvention)valueGenerationConvention); ReplaceConvention(conventionSet.EntityTypePrimaryKeyChangedConventions, valueGenerationConvention); conventionSet.KeyAddedConventions.Add(sqlServerInMemoryTablesConvention); ReplaceConvention(conventionSet.ForeignKeyAddedConventions, valueGenerationConvention); ReplaceConvention(conventionSet.ForeignKeyRemovedConventions, valueGenerationConvention); conventionSet.IndexAddedConventions.Add(sqlServerInMemoryTablesConvention); conventionSet.IndexAddedConventions.Add(sqlServerIndexConvention); conventionSet.IndexUniquenessChangedConventions.Add(sqlServerIndexConvention); conventionSet.IndexAnnotationChangedConventions.Add(sqlServerIndexConvention); conventionSet.PropertyNullabilityChangedConventions.Add(sqlServerIndexConvention); StoreGenerationConvention storeGenerationConvention = new SqlServerStoreGenerationConvention(Dependencies, RelationalDependencies); conventionSet.PropertyAnnotationChangedConventions.Add(sqlServerIndexConvention); ReplaceConvention(conventionSet.PropertyAnnotationChangedConventions, storeGenerationConvention); ReplaceConvention( conventionSet.PropertyAnnotationChangedConventions, (RelationalValueGenerationConvention)valueGenerationConvention); ConventionSet.AddBefore( conventionSet.ModelFinalizedConventions, valueGenerationStrategyConvention, typeof(ValidatingConvention)); ConventionSet.AddBefore( conventionSet.ModelFinalizedConventions, new SqlServerEnumConvention(Dependencies), typeof(ValidatingConvention)); ReplaceConvention(conventionSet.ModelFinalizedConventions, storeGenerationConvention); return(conventionSet); }
public void OnPropertyNullableChanged_calls_apply_on_conventions_in_order(bool useBuilder) { 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); var propertyBuilder = builder.Entity<Order>().Property(e => e.Name); if (useBuilder) { propertyBuilder.IsRequired(); } else { propertyBuilder.Metadata.IsNullable = false; } Assert.Equal(new bool?[] { false }, convention1.Calls); Assert.Equal(new bool?[] { false }, convention2.Calls); Assert.Empty(convention3.Calls); propertyBuilder = builder.Entity<Order>().Property(e => e.Name); if (useBuilder) { propertyBuilder.IsRequired(false); } else { propertyBuilder.Metadata.IsNullable = true; } Assert.Equal(new bool?[] { false, true }, convention1.Calls); Assert.Equal(new bool?[] { false, true }, convention2.Calls); Assert.Empty(convention3.Calls); propertyBuilder = builder.Entity<Order>().Property(e => e.Name); if (useBuilder) { propertyBuilder.IsRequired(false); } else { propertyBuilder.Metadata.IsNullable = true; } Assert.Equal(new bool?[] { false, true }, convention1.Calls); Assert.Equal(new bool?[] { false, true }, convention2.Calls); Assert.Empty(convention3.Calls); propertyBuilder = builder.Entity<Order>().Property(e => e.Name); if (useBuilder) { propertyBuilder.IsRequired(); } else { propertyBuilder.Metadata.IsNullable = false; } Assert.Equal(new bool?[] { false, true, false }, convention1.Calls); Assert.Equal(new bool?[] { false, true, false }, convention2.Calls); Assert.Empty(convention3.Calls); }
public MaterializingModel(ConventionSet conventions) : base(conventions) { }
public void OnPrimaryKeySet_calls_apply_on_conventions_in_order(bool useBuilder) { 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); if (useBuilder) { Assert.NotNull(entityBuilder.PrimaryKey(new[] { "OrderId" }, ConfigurationSource.Convention)); } else { Assert.NotNull(entityBuilder.Metadata.SetPrimaryKey( entityBuilder.Property("OrderId", ConfigurationSource.Convention).Metadata)); } Assert.NotNull(internalKeyBuilder); }
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 = entityBuilder.Metadata.AddForeignKey( new[] { entityBuilder.Property("FK", typeof(int), ConfigurationSource.Convention).Metadata }, entityBuilder.HasKey(new[] { "OrderId" }, ConfigurationSource.Convention).Metadata, entityBuilder.Metadata); Assert.NotNull(entityBuilder.Metadata.RemoveForeignKey(foreignKey.Properties, foreignKey.PrincipalKey, foreignKey.PrincipalEntityType)); Assert.True(foreignKeyRemoved); }
/// <summary> /// <para> /// Call this method to build a <see cref="ConventionSet" /> for SQL Server when using /// the <see cref="ModelBuilder" /> outside of <see cref="DbContext.OnModelCreating" />. /// </para> /// <para> /// Note that it is unusual to use this method. /// Consider using <see cref="DbContext" /> in the normal way instead. /// </para> /// </summary> /// <returns> The convention set. </returns> public static ConventionSet Build() { using var serviceScope = CreateServiceScope(); using var context = serviceScope.ServiceProvider.GetRequiredService <DbContext>(); return(ConventionSet.CreateConventionSet(context)); }