-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDbContext.cs
More file actions
50 lines (44 loc) · 1.63 KB
/
Copy pathAppDbContext.cs
File metadata and controls
50 lines (44 loc) · 1.63 KB
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
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
namespace EF_Conversion_Default
{
internal class AppDbContext : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=localhost\\SQLEXPRESS;Database=EF_Test;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().Property(p => p.Locations)
.HasConversion(
v => JoinOrDefault(v),
v => v.Split(";", StringSplitOptions.RemoveEmptyEntries).Select(val => Enum.Parse<Location>(val)).ToList(),
new ValueComparer<IEnumerable<Location>>(
(c1, c2) => Mycomp(c1, c2),
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
c => c))
.HasDefaultValue(new List<Location> { Location.Cluj });
}
static bool Mycomp(IEnumerable<Location>? a, IEnumerable<Location>? b)
{
if (a == null || b == null) return false;
return a.SequenceEqual(b);
}
static string JoinOrDefault(IEnumerable<Location> v)
{
if (!v.Any())
{
return Location.Cluj.ToString();
}
return string.Join(";", v);
}
}
public enum Location
{
Cluj = 1,
Sibiu = 2,
Brasov = 3
}
}