-
-
Save payoff/77730d63ace9ba6d2a3d4712c7fa2ee8 to your computer and use it in GitHub Desktop.
PrimitiveCollection of Dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore; | |
using Microsoft.EntityFrameworkCore.Design; | |
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | |
public class TestContextFactory : IDesignTimeDbContextFactory<TestContext> | |
{ | |
public TestContext CreateDbContext(string[] args) | |
{ | |
var optionsBuilder = new DbContextOptionsBuilder<TestContext>(); | |
optionsBuilder.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=password"); | |
return new TestContext(optionsBuilder.Options); | |
} | |
} | |
public class TestContext : DbContext | |
{ | |
public TestContext(DbContextOptions options) : base(options) | |
{ | |
} | |
public DbSet<TestEntity> Entities { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder builder) | |
{ | |
builder.UseNpgsql("ConnectionString"); | |
} | |
protected override void OnModelCreating(ModelBuilder builder) | |
{ | |
builder.Entity<TestEntity>(b => | |
{ | |
b.PrimitiveCollection(e => e.AllowedTransitions) | |
.ElementType() | |
.HasConversion(new KeyValueListStringsConverter()); | |
}); | |
} | |
} | |
public class TestEntity | |
{ | |
public int Id { get; set; } | |
public Dictionary<string, List<string>> AllowedTransitions { get; set; } = new(); | |
} | |
public class KeyValueListStringsConverter : ValueConverter<KeyValuePair<string, List<string>?>?, string?> | |
{ | |
public KeyValueListStringsConverter() | |
: base(value => ConvertTo(value), value => FromProvider(value)) | |
{ | |
} | |
private static string? ConvertTo(KeyValuePair<string, List<string>?>? kv) | |
{ | |
if (kv?.Key == null) return null; | |
var items = kv.Value.Value == null ? null : string.Join("|", kv.Value.Value); | |
return $"{kv.Value.Key}:{items}"; | |
} | |
private static KeyValuePair<string, List<string>?>? FromProvider(string? value) | |
{ | |
if (string.IsNullOrEmpty(value)) return null; | |
var parts = value.Split(':', 2); | |
if (parts.Length != 2) return new KeyValuePair<string, List<string>?>(parts[0], new List<string>()); | |
return new KeyValuePair<string, List<string>?>(parts[0], parts[1].Split('|').ToList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment