-
Notifications
You must be signed in to change notification settings - Fork 2k
/
CaseInsensitiveStrings.cs
107 lines (89 loc) · 3.36 KB
/
CaseInsensitiveStrings.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
102
103
104
105
106
107
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace EFModeling.ValueConversions;
public class CaseInsensitiveStrings : Program
{
public void Run()
{
ConsoleWriteLines("Sample showing value conversions for case-insensitive string keys...");
using (var context = new SampleDbContext())
{
CleanDatabase(context);
ConsoleWriteLines("Save new entities...");
context.AddRange(
new Blog
{
Id = "dotnet",
Name = ".NET Blog",
},
new Post
{
Id = "post1",
BlogId = "dotnet",
Title = "Some good .NET stuff"
},
new Post
{
Id = "Post2",
BlogId = "DotNet",
Title = "Some more good .NET stuff"
});
context.SaveChanges();
}
using (var context = new SampleDbContext())
{
ConsoleWriteLines("Read the entities back...");
var blog = context.Set<Blog>().Include(e => e.Posts).Single();
ConsoleWriteLines($"The blog has {blog.Posts.Count} posts with foreign keys '{blog.Posts.First().BlogId}' and '{blog.Posts.Skip(1).First().BlogId}'");
}
ConsoleWriteLines("Sample finished.");
}
public class SampleDbContext : DbContext
{
#region ConfigureCaseInsensitiveStrings
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var comparer = new ValueComparer<string>(
(l, r) => string.Equals(l, r, StringComparison.OrdinalIgnoreCase),
v => v.ToUpper().GetHashCode(),
v => v);
modelBuilder.Entity<Blog>()
.Property(e => e.Id)
.Metadata.SetValueComparer(comparer);
modelBuilder.Entity<Post>(
b =>
{
b.Property(e => e.Id).Metadata.SetValueComparer(comparer);
b.Property(e => e.BlogId).Metadata.SetValueComparer(comparer);
});
}
#endregion
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted })
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=CaseInsensitiveStrings;Trusted_Connection=True;ConnectRetryCount=0")
.EnableSensitiveDataLogging();
}
#region CaseInsensitiveStringsModel
public class Blog
{
public string Id { get; set; }
public string Name { get; set; }
public ICollection<Post> Posts { get; set; }
}
public class Post
{
public string Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string BlogId { get; set; }
public Blog Blog { get; set; }
}
#endregion
}