@@ -959,4 +959,104 @@ public void Should_project_ok()
959959 }
960960 }
961961 }
962+ public class MemberWithSubQueryIdentity : AutoMapperSpecBase
963+ {
964+ protected override MapperConfiguration CreateConfiguration ( ) => new MapperConfiguration ( cfg =>
965+ {
966+ cfg . CreateProjection < AEntity , Dto > ( )
967+ . ForMember ( dst => dst . DtoSubWrapper , opt => opt . MapFrom ( src => src ) ) ;
968+ cfg . CreateProjection < AEntity , DtoSubWrapper > ( )
969+ . ForMember ( dst => dst . DtoSub , opt => opt . MapFrom ( src => src . BEntity . CEntities . FirstOrDefault ( x => x . Id == src . CEntityId ) ) ) ;
970+ cfg . CreateProjection < CEntity , DtoSub > ( ) ;
971+ } ) ;
972+ [ Fact ]
973+ public void Should_work ( )
974+ {
975+ var query = ProjectTo < Dto > ( new ClientContext ( ) . AEntities ) ;
976+ var result = query . Single ( ) ;
977+ result . DtoSubWrapper . DtoSub . ShouldNotBeNull ( ) ;
978+ result . DtoSubWrapper . DtoSub . SubString . ShouldBe ( "Test" ) ;
979+ }
980+ public class Dto
981+ {
982+ public int Id { get ; set ; }
983+ public DtoSubWrapper DtoSubWrapper { get ; set ; }
984+ }
985+ public class DtoSubWrapper
986+ {
987+ public DtoSub DtoSub { get ; set ; }
988+ }
989+ public class DtoSub
990+ {
991+ public int Id { get ; set ; }
992+ public string SubString { get ; set ; }
993+ }
994+ public class AEntity
995+ {
996+ public int Id { get ; set ; }
997+ public int BEntityId { get ; set ; }
998+ public int CEntityId { get ; set ; }
999+ public BEntity BEntity { get ; set ; }
1000+ }
1001+ public class BEntity
1002+ {
1003+ public int Id { get ; set ; }
1004+ public ICollection < CEntity > CEntities { get ; set ; }
1005+ }
1006+ public class CEntity
1007+ {
1008+ public int Id { get ; set ; }
1009+ public int BEntityId { get ; set ; }
1010+ public string SubString { get ; set ; }
1011+ public BEntity BEntity { get ; set ; }
1012+ }
1013+ class Initializer : DropCreateDatabaseAlways < ClientContext >
1014+ {
1015+ protected override void Seed ( ClientContext context )
1016+ {
1017+ context . AEntities . Add ( new AEntity
1018+ {
1019+ Id = 1 ,
1020+ BEntityId = 1 ,
1021+ CEntityId = 6 ,
1022+ BEntity = new BEntity
1023+ {
1024+ Id = 1 ,
1025+ CEntities = new List < CEntity >
1026+ {
1027+ new CEntity
1028+ {
1029+ Id = 6 ,
1030+ BEntityId = 1 ,
1031+ SubString = "Test"
1032+ }
1033+ }
1034+ } ,
1035+ } ) ;
1036+ }
1037+ }
1038+ class ClientContext : DbContext
1039+ {
1040+ protected override void OnModelCreating ( DbModelBuilder modelBuilder )
1041+ {
1042+ Database . Log = s => System . Diagnostics . Debug . WriteLine ( s ) ;
1043+ Database . SetInitializer ( new Initializer ( ) ) ;
1044+
1045+ modelBuilder . Entity < AEntity > ( )
1046+ . HasRequired ( x => x . BEntity )
1047+ . WithMany ( )
1048+ . HasForeignKey ( x => x . BEntityId ) ;
1049+
1050+ modelBuilder . Entity < BEntity > ( )
1051+ . HasMany ( x => x . CEntities )
1052+ . WithRequired ( x => x . BEntity )
1053+ . HasForeignKey ( x => x . BEntityId ) ;
1054+
1055+ modelBuilder . Entity < CEntity > ( )
1056+ . Property ( x => x . Id )
1057+ . HasDatabaseGeneratedOption ( DatabaseGeneratedOption . None ) ;
1058+ }
1059+ public DbSet < AEntity > AEntities { get ; set ; }
1060+ }
1061+ }
9621062}
0 commit comments