-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Including docs for new value comparer fluent API
- Loading branch information
Showing
6 changed files
with
140 additions
and
66 deletions.
There are no files selected for viewing
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
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
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
92 changes: 92 additions & 0 deletions
92
samples/core/Modeling/ValueConversions/MappingListPropertyOld.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace EFModeling.ValueConversions | ||
{ | ||
public class MappingListPropertyOld : Program | ||
{ | ||
public void Run() | ||
{ | ||
ConsoleWriteLines("Sample showing value conversions for a List<int>..."); | ||
|
||
using (var context = new SampleDbContext()) | ||
{ | ||
CleanDatabase(context); | ||
|
||
ConsoleWriteLines("Save a new entity..."); | ||
|
||
var entity = new EntityType { MyListProperty = new List<int> { 1, 2, 3 } }; | ||
context.Add(entity); | ||
context.SaveChanges(); | ||
|
||
ConsoleWriteLines("Mutate the property value and save again..."); | ||
|
||
// This will be detected and EF will update the database on SaveChanges | ||
entity.MyListProperty.Add(4); | ||
|
||
context.SaveChanges(); | ||
} | ||
|
||
using (var context = new SampleDbContext()) | ||
{ | ||
ConsoleWriteLines("Read the entity back..."); | ||
|
||
var entity = context.Set<EntityType>().Single(); | ||
|
||
Debug.Assert(entity.MyListProperty.SequenceEqual(new List<int> { 1, 2, 3, 4 })); | ||
} | ||
|
||
ConsoleWriteLines("Sample finished."); | ||
} | ||
|
||
public class SampleDbContext : DbContext | ||
{ | ||
private static readonly ILoggerFactory | ||
Logger = LoggerFactory.Create(x => x.AddConsole()); //.SetMinimumLevel(LogLevel.Debug)); | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
#region ConfigureListProperty | ||
modelBuilder | ||
.Entity<EntityType>() | ||
.Property(e => e.MyListProperty) | ||
.HasConversion( | ||
v => JsonSerializer.Serialize(v, null), | ||
v => JsonSerializer.Deserialize<List<int>>(v, null)); | ||
|
||
var valueComparer = new ValueComparer<List<int>>( | ||
(c1, c2) => c1.SequenceEqual(c2), | ||
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())), | ||
c => c.ToList()); | ||
|
||
modelBuilder | ||
.Entity<EntityType>() | ||
.Property(e => e.MyListProperty) | ||
.Metadata | ||
.SetValueComparer(valueComparer); | ||
#endregion | ||
} | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
=> optionsBuilder | ||
.UseLoggerFactory(Logger) | ||
.UseSqlite("DataSource=test.db") | ||
.EnableSensitiveDataLogging(); | ||
} | ||
|
||
public class EntityType | ||
{ | ||
public int Id { get; set; } | ||
|
||
#region ListProperty | ||
public List<int> MyListProperty { get; set; } | ||
#endregion | ||
} | ||
} | ||
} |
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
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