Skip to content

Commit ec3b19f

Browse files
committed
Test for mapping resolving when the mapping object not is specified during configuration and have circular references with EF.
1 parent 702ce32 commit ec3b19f

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
using System.Data.Entity;
5+
using System.Data.SqlServerCe;
6+
using System.Linq;
7+
using AutoMapper.EntityFramework;
8+
using AutoMapper.EquivalencyExpression;
9+
using FluentAssertions;
10+
using Xunit;
11+
12+
namespace AutoMapper.Collection.EntityFramework.Tests
13+
{
14+
public class EntityFramworkUnmappedTypes
15+
{
16+
private void ConfigureMapper(IMapperConfigurationExpression cfg)
17+
{
18+
cfg.AddCollectionMappers();
19+
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DB>>();
20+
}
21+
22+
protected IMapper CreateMapper(Action<IMapperConfigurationExpression> cfg)
23+
{
24+
var map = new MapperConfiguration(cfg);
25+
map.CompileMappings();
26+
27+
var mapper = map.CreateMapper();
28+
mapper.ConfigurationProvider.AssertConfigurationIsValid();
29+
return mapper;
30+
}
31+
32+
33+
[Fact]
34+
public void Should_not_throw_exception_for_nonexisting_types()
35+
{
36+
var mapper = CreateMapper(ConfigureMapper);
37+
38+
var originalModel = new System
39+
{
40+
Name = "My First System",
41+
Contacts = new List<Contact>
42+
{
43+
new Contact
44+
{
45+
Name = "John",
46+
Emails = new List<Email>()
47+
{
48+
new Email
49+
{
50+
Address = "[email protected]"
51+
}
52+
}
53+
}
54+
}
55+
};
56+
57+
var originalEmail = originalModel.Contacts.Single().Emails.Single();
58+
59+
var assertModel = mapper.Map<SystemViewModel>(originalModel);
60+
assertModel.Name.Should().Be(originalModel.Name);
61+
assertModel.Contacts.Single().Name.Should().Be(originalModel.Contacts.Single().Name);
62+
assertModel.Contacts.Single().Emails.Single().Address.Should().Be(originalModel.Contacts.Single().Emails.Single().Address);
63+
64+
assertModel.Contacts.Single().Emails.Add(new EmailViewModel { Address = "[email protected]" });
65+
66+
mapper.Map(assertModel, originalModel);
67+
// This tests if equality was found and mapped to pre-existing object and not defaulting to AM and clearing and regenerating the list
68+
originalModel.Contacts.Single().Emails.First().Should().Be(originalEmail);
69+
}
70+
71+
public class DB : DbContext
72+
{
73+
public DB()
74+
: base(new SqlCeConnection("Data Source=MyDatabase.sdf;Persist Security Info=False;"), contextOwnsConnection: true)
75+
{
76+
}
77+
78+
public DbSet<System> Systems { get; set; }
79+
public DbSet<Contact> Contacts { get; set; }
80+
public DbSet<Email> Emails { get; set; }
81+
}
82+
83+
84+
public class System
85+
{
86+
public Guid Id { get; set; } = Guid.NewGuid();
87+
public string Name { get; set; }
88+
89+
public ICollection<Contact> Contacts { get; set; }
90+
}
91+
92+
public class Contact
93+
{
94+
public Guid Id { get; set; } = Guid.NewGuid();
95+
public Guid SystemId { get; set; }
96+
public string Name { get; set; }
97+
98+
[ForeignKey("SystemId")]
99+
public System System { get; set; }
100+
101+
public ICollection<Email> Emails { get; set; }
102+
}
103+
104+
public class Email
105+
{
106+
public Guid Id { get; set; } = Guid.NewGuid();
107+
108+
public Guid ContactId { get; set; }
109+
public string Address { get; set; }
110+
111+
[ForeignKey("ContactId")]
112+
public Contact Contact { get; set; }
113+
}
114+
115+
public class SystemViewModel
116+
{
117+
public Guid Id { get; set; }
118+
public string Name { get; set; }
119+
120+
public ICollection<ContactViewModel> Contacts { get; set; }
121+
}
122+
123+
public class ContactViewModel
124+
{
125+
public Guid Id { get; set; }
126+
public Guid SystemId { get; set; }
127+
public string Name { get; set; }
128+
129+
public SystemViewModel System { get; set; }
130+
131+
public ICollection<EmailViewModel> Emails { get; set; }
132+
}
133+
134+
public class EmailViewModel
135+
{
136+
public Guid Id { get; set; }
137+
public Guid ContactId { get; set; }
138+
public string Address { get; set; }
139+
140+
public ContactViewModel Contact { get; set; }
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)