@@ -640,12 +640,16 @@ protected virtual void ValidateInheritanceMapping(
640
640
/// <param name="rootEntityType">The entity type to validate.</param>
641
641
protected virtual void ValidateDiscriminatorValues ( IEntityType rootEntityType )
642
642
{
643
- var derivedTypes = rootEntityType . GetDerivedTypesInclusive ( ) . ToList ( ) ;
643
+ var derivedTypes = rootEntityType . GetDerivedTypesInclusive ( ) ;
644
644
var discriminatorProperty = rootEntityType . FindDiscriminatorProperty ( ) ;
645
645
if ( discriminatorProperty == null )
646
646
{
647
- if ( derivedTypes . Count == 1 )
647
+ if ( ! derivedTypes . Skip ( 1 ) . Any ( ) )
648
648
{
649
+ foreach ( var complexProperty in rootEntityType . GetDeclaredComplexProperties ( ) )
650
+ {
651
+ ValidateDiscriminatorValues ( complexProperty . ComplexType ) ;
652
+ }
649
653
return ;
650
654
}
651
655
@@ -654,6 +658,68 @@ protected virtual void ValidateDiscriminatorValues(IEntityType rootEntityType)
654
658
}
655
659
656
660
var discriminatorValues = new Dictionary < object , IEntityType > ( discriminatorProperty . GetKeyValueComparer ( ) ) ;
661
+ foreach ( var derivedType in derivedTypes )
662
+ {
663
+ foreach ( var complexProperty in derivedType . GetDeclaredComplexProperties ( ) )
664
+ {
665
+ ValidateDiscriminatorValues ( complexProperty . ComplexType ) ;
666
+ }
667
+
668
+ if ( ! derivedType . ClrType . IsInstantiable ( ) )
669
+ {
670
+ continue ;
671
+ }
672
+
673
+ var discriminatorValue = derivedType [ CoreAnnotationNames . DiscriminatorValue ] ;
674
+ if ( discriminatorValue == null )
675
+ {
676
+ throw new InvalidOperationException (
677
+ CoreStrings . NoDiscriminatorValue ( derivedType . DisplayName ( ) ) ) ;
678
+ }
679
+
680
+ if ( ! discriminatorProperty . ClrType . IsInstanceOfType ( discriminatorValue ) )
681
+ {
682
+ throw new InvalidOperationException (
683
+ CoreStrings . DiscriminatorValueIncompatible (
684
+ discriminatorValue , derivedType . DisplayName ( ) , discriminatorProperty . ClrType . DisplayName ( ) ) ) ;
685
+ }
686
+
687
+ if ( discriminatorValues . TryGetValue ( discriminatorValue , out var duplicateEntityType ) )
688
+ {
689
+ throw new InvalidOperationException (
690
+ CoreStrings . DuplicateDiscriminatorValue (
691
+ derivedType . DisplayName ( ) , discriminatorValue , duplicateEntityType . DisplayName ( ) ) ) ;
692
+ }
693
+
694
+ discriminatorValues [ discriminatorValue ] = derivedType ;
695
+ }
696
+ }
697
+
698
+ /// <summary>
699
+ /// Validates the discriminator and values for the given complex type and nested ones.
700
+ /// </summary>
701
+ /// <param name="complexType">The entity type to validate.</param>
702
+ protected virtual void ValidateDiscriminatorValues ( IComplexType complexType )
703
+ {
704
+ foreach ( var complexProperty in complexType . GetComplexProperties ( ) )
705
+ {
706
+ ValidateDiscriminatorValues ( complexProperty . ComplexType ) ;
707
+ }
708
+
709
+ var derivedTypes = complexType . GetDerivedTypesInclusive ( ) ;
710
+ var discriminatorProperty = complexType . FindDiscriminatorProperty ( ) ;
711
+ if ( discriminatorProperty == null )
712
+ {
713
+ if ( ! derivedTypes . Skip ( 1 ) . Any ( ) )
714
+ {
715
+ return ;
716
+ }
717
+
718
+ throw new InvalidOperationException (
719
+ CoreStrings . NoDiscriminatorProperty ( complexType . DisplayName ( ) ) ) ;
720
+ }
721
+
722
+ var discriminatorValues = new Dictionary < object , IComplexType > ( discriminatorProperty . GetKeyValueComparer ( ) ) ;
657
723
658
724
foreach ( var derivedType in derivedTypes )
659
725
{
0 commit comments