Skip to content

Commit 2700e61

Browse files
committed
Add test for ef core issue 13
1 parent 79126e3 commit 2700e61

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System.Collections.Generic;
2+
using AutoMapper.EquivalencyExpression;
3+
using FluentAssertions;
4+
using Xunit;
5+
6+
namespace AutoMapper.Collection
7+
{
8+
public class NotExistingTests
9+
{
10+
[Fact]
11+
public void Should_not_throw_exception_for_nonexisting_types()
12+
{
13+
var configuration = new MapperConfiguration(x =>
14+
{
15+
//x.CreateMissingTypeMaps = false;
16+
x.AddCollectionMappers();
17+
});
18+
IMapper mapper = new Mapper(configuration);
19+
20+
var system = new System
21+
{
22+
Name = "My First System",
23+
Contacts = new List<Contact>
24+
{
25+
new Contact
26+
{
27+
Name = "John",
28+
Emails = new List<Email>()
29+
{
30+
new Email
31+
{
32+
Address = "[email protected]"
33+
}
34+
}
35+
}
36+
}
37+
};
38+
39+
mapper.Map<SystemViewModel>(system);
40+
}
41+
42+
public class System
43+
{
44+
public int Id { get; set; }
45+
public string Name { get; set; }
46+
47+
public ICollection<Contact> Contacts { get; set; }
48+
}
49+
50+
public class Contact
51+
{
52+
public int Id { get; set; }
53+
public int SystemId { get; set; }
54+
public string Name { get; set; }
55+
56+
public System System { get; set; }
57+
58+
public ICollection<Email> Emails { get; set; }
59+
}
60+
61+
public class Email
62+
{
63+
public int Id { get; set; }
64+
65+
public int ContactId { get; set; }
66+
public string Address { get; set; }
67+
68+
public Contact Contact { get; set; }
69+
}
70+
71+
public class SystemViewModel
72+
{
73+
public int Id { get; set; }
74+
public string Name { get; set; }
75+
76+
public ICollection<ContactViewModel> Contacts { get; set; }
77+
}
78+
79+
public class ContactViewModel
80+
{
81+
public int Id { get; set; }
82+
public int SystemId { get; set; }
83+
public string Name { get; set; }
84+
85+
public SystemViewModel System { get; set; }
86+
87+
public ICollection<EmailViewModel> Emails { get; set; }
88+
}
89+
90+
public class EmailViewModel
91+
{
92+
public int Id { get; set; }
93+
public int ContactId { get; set; }
94+
public string Address { get; set; }
95+
96+
public ContactViewModel Contact { get; set; }
97+
}
98+
}
99+
}

src/AutoMapper.Collection/Mappers/EquivalentExpressionAddRemoveCollectionMapper.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,16 @@ public static TDestination Map<TSource, TSourceItem, TDestination, TDestinationI
6666

6767
public bool IsMatch(TypePair typePair)
6868
{
69-
return typePair.SourceType.IsEnumerableType()
70-
&& typePair.DestinationType.IsCollectionType()
71-
&& this.GetEquivalentExpression(TypeHelper.GetElementType(typePair.SourceType), TypeHelper.GetElementType(typePair.DestinationType)) != null;
69+
if (typePair.SourceType.IsEnumerableType()
70+
&& typePair.DestinationType.IsCollectionType())
71+
{
72+
var realType = new TypePair(TypeHelper.GetElementType(typePair.SourceType), TypeHelper.GetElementType(typePair.DestinationType));
73+
74+
return realType != typePair
75+
&& this.GetEquivalentExpression(realType.SourceType, realType.DestinationType) != null;
76+
}
77+
78+
return false;
7279
}
7380

7481
public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, IMemberMap memberMap,

0 commit comments

Comments
 (0)