-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
using_natural_identity_keys.cs
101 lines (84 loc) · 2.55 KB
/
using_natural_identity_keys.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using JasperFx.Core;
using JasperFx.Core.Reflection;
using Marten.Schema;
using Marten.Schema.Identity;
using Marten.Testing.Harness;
using Shouldly;
using Xunit;
namespace DocumentDbTests.Writing.Identity;
public class using_natural_identity_keys: IntegrationContext
{
[Fact]
public void finds_the_id_member_with_the_attribute_on_a_field()
{
var mapping = DocumentMapping.For<NonStandardDoc>();
mapping.IdMember.Name.ShouldBe(nameof(NonStandardDoc.Name));
}
[Fact]
public void finds_the_id_member_with_the_attribute_on_a_property()
{
var mapping = DocumentMapping.For<NonStandardWithProp>();
mapping.IdMember.Name.ShouldBe(nameof(NonStandardWithProp.Name));
}
[Fact]
public void finds_the_right_id_member_for_doc_with_both_id_column_and_identity_attribute()
{
var mapping = DocumentMapping.For<IdAndIdentityAttDoc>();
mapping.IdMember.Name.ShouldBe(nameof(IdAndIdentityAttDoc.DocumentId));
}
[Fact]
public void can_persist_with_natural_key()
{
var doc = new NonStandardDoc { Name = "somebody" };
using (var session = theStore.LightweightSession())
{
session.Store(doc);
session.SaveChanges();
}
using (var query = theStore.QuerySession())
{
SpecificationExtensions.ShouldNotBeNull(query.Load<NonStandardDoc>("somebody"));
}
}
[Fact]
public void can_override_the_identity_member_with_the_fluent_interface()
{
StoreOptions(storeOptions =>
{
#region sample_sample-override-id-fluent-interance
storeOptions.Schema.For<OverriddenIdDoc>().Identity(x => x.Name);
#endregion
});
var mapping = theStore.StorageFeatures.MappingFor(typeof(OverriddenIdDoc)).As<DocumentMapping>();
mapping.IdType.ShouldBe(typeof(string));
mapping.IdMember.Name.ShouldBe(nameof(OverriddenIdDoc.Name));
mapping.IdStrategy.ShouldBeOfType<StringIdGeneration>();
}
public using_natural_identity_keys(DefaultStoreFixture fixture) : base(fixture)
{
}
}
#region sample_IdentityAttribute
public class NonStandardDoc
{
[Identity]
public string Name;
}
#endregion
public class NonStandardWithProp
{
[Identity]
public string Name { get; set; }
}
public class OverriddenIdDoc
{
public string Name { get; set; }
public DateTime Date { get; set; }
}
public class IdAndIdentityAttDoc
{
public Guid Id { get; set; }
[Identity]
public Guid DocumentId { get; set; }
}