-
Notifications
You must be signed in to change notification settings - Fork 22
/
ConfigureFromServicesTests.cs
69 lines (55 loc) · 2.39 KB
/
ConfigureFromServicesTests.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
using Detached.Mappers.Annotations.Extensions;
using Detached.Mappers.EntityFramework.Extensions;
using Detached.Mappers.EntityFramework.Tests.Fixture;
using Detached.Mappers.Types;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Detached.Mappers.EntityFramework.Tests.Configuration
{
public class ConfigureFromServicesTests
{
[Fact]
public void configure_mapper_from_services()
{
var services = new ServiceCollection();
services.AddDbContext<ModuleTestDbContext>((services, db) =>
{
db.UseSqlite($"DataSource=file:{nameof(configure_mapper_from_services)}?mode=memory&cache=shared");
db.UseMapping(services);
});
services.ConfigureMapper<ModuleTestDbContext>(builder =>
{
builder.Type<ModuleTestClass>().Entity(true);
builder.Type<ModuleTestClass>().Member(c => c.CustomizedKey1).Key(true);
builder.Type<ModuleTestClass>().Member(c => c.CustomizedKey2).Key(true);
builder.Type<ModuleTestClass>().Key(c => c.CustomizedKey1, c => c.CustomizedKey2);
});
var serviceProvider = services.BuildServiceProvider();
var dbContext = serviceProvider.GetRequiredService<ModuleTestDbContext>();
IType typeOptions = dbContext.GetMapper().Options.GetType(typeof(ModuleTestClass));
Assert.True(typeOptions.IsEntity());
Assert.True(typeOptions.GetMember(nameof(ModuleTestClass.CustomizedKey1)).IsKey());
Assert.True(typeOptions.GetMember(nameof(ModuleTestClass.CustomizedKey2)).IsKey());
Assert.False(typeOptions.GetMember(nameof(ModuleTestClass.Id)).IsKey());
}
}
public class ModuleTestClass
{
public int Id { get; set; }
public int CustomizedKey1 { get; set; }
public int CustomizedKey2 { get; set; }
public string Name { get; set; }
}
public class ModuleTestDbContext : TestDbContext
{
public ModuleTestDbContext(DbContextOptions<ModuleTestDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder mb)
{
mb.Entity<ModuleTestClass>().HasKey(c => new { c.CustomizedKey1, c.CustomizedKey2 });
}
}
}