Pomelo JSON columns do not track changes?

87 views Asked by At

Question time people!

I am trying to implement a JSON column with Pomelo and it worked like a charm until I realized it did not work with the EF's change detection.

The entities look like this.

public class Child
{
    public string name {get;set;}
}
public class Entity
{
    public List<Child> Children {get;set;}
}

The dbcontext maps the Entity to a json column.

entity.Property(e => e.Children)
                .HasColumnType("json")

Adding a new Entity works and so does retrieving an Entity. The json gets converted to and from the list very nicely.

But when you add an element to the Children list and call SaveChanges(), EF acts as if they do not know of any changes! However, if you explicitly call dbContext.Update(entity) and shove the entity down EF's throat, then it updates.

Any idea why this happens?

I found this bug which reports a similar scenario but it has since been fixed.

1

There are 1 answers

0
Chin. Udara On

@Svyatoslav thank you for the suggestion. The link you had provided was very helpful.

As it turns out, there is a neat little option that we can use so that EF will actually bother enough to track the changes of a JSON column.

One can either set this option globally or per property. I could not get the global settings to work, however the property level settings did it for me!

entity.Property(e => e.Children)
.HasColumnType("json")
.HasColumnName("Children")
 // tell EF how to track the changes
.UseJsonChangeTrackingOptions(
    MySqlCommonJsonChangeTrackingOptions.FullHierarchySemantically
 );