-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathBindForeingKeyToDto.cs
86 lines (68 loc) · 2.29 KB
/
BindForeingKeyToDto.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Detached.Mappers.EntityFramework.Extensions;
using Detached.Mappers.EntityFramework.Tests.Fixture;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Detached.Mappers.EntityFramework.Tests.Features
{
public class BindForeignKeyToDto
{
[Fact]
public async Task bind_fk_to_dto()
{
var dbContext = await TestDbContext.Create<BindFkToDtoDbContext>();
dbContext.Parents.Add(new ParentEntity
{
Id = 1,
Name = "Parent 1",
Child = new ChildEntity
{
Id = 1,
Name = "Child 1"
},
Children = new List<ChildEntity>
{
new ChildEntity { Id = 2, Name = "Child 2"},
new ChildEntity { Id = 3, Name = "Child 3"}
}
});
await dbContext.SaveChangesAsync();
var result = dbContext
.Project<ParentEntity, ParentDto>(dbContext.Parents)
.FirstOrDefault();
Assert.Equal(1, result.ChildId);
Assert.Equal(2, result.ChildIds[0]);
Assert.Equal(3, result.ChildIds[1]);
}
public class ParentDto
{
public int Id { get; set; }
public string Name { get; set; }
public int ChildId { get; set; }
public List<int> ChildIds { get; set; }
}
public class ParentEntity
{
public int Id { get; set; }
public string Name { get; set; }
public ChildEntity Child { get; set; }
public List<ChildEntity> Children { get; set; }
}
public class ChildEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
public class BindFkToDtoDbContext : TestDbContext
{
public BindFkToDtoDbContext(DbContextOptions<BindFkToDtoDbContext> options)
: base(options)
{
}
public DbSet<ParentEntity> Parents { get; set; }
public DbSet<ChildEntity> Children { get; set; }
}
}
}