-
-
Save alexhiggins732/b32de695193d3e272ec0a792d84c89d9 to your computer and use it in GitHub Desktop.
How to map a dictionary property of an entity to a string field of a table in Entity Framework Core v2.1+
This file contains hidden or 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
public class MyDbContext : DbContext | |
{ | |
public DbSet<User> Users { get; set; } | |
public MyDbContext(DbContextOptions<MyDbContext> options) | |
: base(options) | |
{ | |
} | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
base.OnModelCreating(modelBuilder); | |
modelBuilder.Entity<User>(b => | |
{ | |
b.Property(u => u.UserName).IsRequired().HasMaxLength(128); | |
b.Property(u => u.ExtraProperties) | |
.HasConversion( | |
d => JsonConvert.SerializeObject(d, Formatting.None), | |
s => JsonConvert.DeserializeObject<Dictionary<string, object>>(s) | |
) | |
.HasMaxLength(4000) | |
.IsRequired(); | |
}); | |
} | |
} |
This file contains hidden or 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
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
using (var context = new MyDbContext(CreateDbContextOptions())) | |
{ | |
var userJohn = new User("john"); | |
var userNeo = new User("neo") | |
{ | |
ExtraProperties = | |
{ | |
["Age"] = 42 | |
} | |
}; | |
context.Users.Add(userJohn); | |
context.Users.Add(userNeo); | |
context.SaveChanges(); | |
} | |
using (var context = new MyDbContext(CreateDbContextOptions())) | |
{ | |
foreach (var user in context.Users) | |
{ | |
Console.WriteLine(user); | |
} | |
} | |
Console.ReadLine(); | |
} | |
public static DbContextOptions<MyDbContext> CreateDbContextOptions() | |
{ | |
return new DbContextOptionsBuilder<MyDbContext>() | |
.UseSqlServer("Server=localhost;Database=EfCoreValueConverterDemo;Trusted_Connection=True;MultipleActiveResultSets=true") | |
.Options; | |
} | |
} |
This file contains hidden or 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
public class User | |
{ | |
public Guid Id { get; set; } | |
public string UserName { get; set; } | |
public Dictionary<string, object> ExtraProperties { get; set; } | |
public User() | |
{ | |
Id = Guid.NewGuid(); | |
ExtraProperties = new Dictionary<string, object>(); | |
} | |
public override string ToString() | |
{ | |
return "# User: " + JsonConvert.SerializeObject(this, Formatting.Indented); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment