private static DiscriminatorBuilder DiscriminatorBuilder( IMutableEntityType entityType, [NotNull] PropertyBuilder discriminatorPropertyBuilder) { var rootTypeBuilder = new EntityTypeBuilder(entityType.RootType()); var discriminatorProperty = (IConventionProperty)discriminatorPropertyBuilder.Metadata; // Make sure the property is on the root type discriminatorPropertyBuilder = discriminatorProperty.GetTypeConfigurationSource() != null ? rootTypeBuilder.Property(discriminatorProperty.ClrType, discriminatorProperty.Name) : rootTypeBuilder.Property(discriminatorProperty.Name); var oldDiscriminatorProperty = entityType.GetDiscriminatorProperty() as IConventionProperty; if (oldDiscriminatorProperty?.Builder != null && oldDiscriminatorProperty != discriminatorProperty) { oldDiscriminatorProperty.DeclaringEntityType.Builder.RemoveUnusedShadowProperties( new[] { oldDiscriminatorProperty }); } rootTypeBuilder.Metadata.SetDiscriminatorProperty(discriminatorProperty); discriminatorPropertyBuilder.IsRequired(); discriminatorPropertyBuilder.HasValueGenerator(DiscriminatorValueGenerator.Factory); return(new DiscriminatorBuilder(entityType)); }
/// <summary> /// Sets the <see cref="IProperty" /> that will be used for storing a discriminator value. /// </summary> /// <param name="entityType"> The entity type to set the discriminator property for. </param> /// <param name="property"> The property to set. </param> public static void SetDiscriminatorProperty([NotNull] this IMutableEntityType entityType, [CanBeNull] IProperty property) { CheckDiscriminatorProperty(entityType, property); var oldDiscriminatorType = entityType.GetDiscriminatorProperty()?.ClrType; if (property == null || property.ClrType != oldDiscriminatorType) { foreach (var derivedType in entityType.GetDerivedTypesInclusive()) { derivedType.RemoveDiscriminatorValue(); } } entityType.SetOrRemoveAnnotation(RelationalAnnotationNames.DiscriminatorProperty, property?.Name); }