Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON POCO Save Changes does not update props in JSON #1228

Closed
arif-hanif opened this issue Jan 26, 2020 · 4 comments
Closed

JSON POCO Save Changes does not update props in JSON #1228

arif-hanif opened this issue Jan 26, 2020 · 4 comments

Comments

@arif-hanif
Copy link

Version v3.1.0

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace API.DesignHub.Entities
{
    public class DesignHubProject
    {
        [Key]
        public string Number { get; set; }
        public string Name { get; set; }
        [Column(TypeName = "jsonb")]
        public ProjectSectionStatus SectionStatuses { get; set; }
    }
}
using System.Text.Json.Serialization;

namespace API.DesignHub.Entities
{
    public class ProjectSectionStatus
    {
        public ProjectSectionStage ExecutiveSummarySectionStatus { get; set; } 
        public ProjectSectionStage CodesAndGuidelinesSectionStatus { get; set; }
        public bool IsMechanicalComplete { get; set; } = false;
    }
}
using System.Runtime.Serialization;

namespace API.DesignHub.Entities
{
    public enum ProjectSectionStage
    {
        NOT_STARTED,
        INCOMPLETE,
        COMPLETE,
        NEEDS_REVIEW,
        NOT_APPLICABLE
    }
}
var designHubProject = _dbContext.DesignHubProjects.SingleOrDefault(
                dhp => dhp.Number == projectNumber);

designHubProject.ExecutiveSummarySectionStatus = ProjectSectionStage.COMPLETE

 _dbContext.SaveChanges();

The DB does not get modified

@arif-hanif arif-hanif changed the title JSON POCO Save Changes not updated props JSON POCO Save Changes does not update props in JSON Jan 26, 2020
@bojan96
Copy link

bojan96 commented Feb 3, 2020

I have same issue, updating any json property or adding element to array does not update json object in DB

@roji
Copy link
Member

roji commented Feb 3, 2020

Because JSON documents can be arbitrarily large, we don't enable automatic change detection on them (more precisely, we don't define a value comparer that does value comparison). If we did, that means that would mean that we would do a snapshot of every JSON document loaded from the database, and a deep comparison against the snapshot once SaveChanges is called. This would be extremely bad for perf.

You can manually flag the JSON document as modified yourself:

ctx.Entry(designHubProject).Property(b => b.SectionStatuses).IsModified = true;

Note that your last snippet has designHubProject.ExecutiveSummarySectionStatus = ProjectSectionStage.COMPLETE, but ExecutiveSummarySectionStatus seems to be a property on SectionStatuses.

Note that support for change-tracking proxies has been implemented on the EF Core side; this means that when 5.0 comes out, you may be able to have automated change tracking without snapshot and comparison, by wrapping your JsonDocument/JsonElement by a proxy.

@roji roji closed this as completed Feb 3, 2020
@arif-hanif
Copy link
Author

thanks @roji was able to implement what you pointed out
ctx.Entry(designHubProject).Property(b => b.SectionStatuses).IsModified = true;

@TsengSR
Copy link

TsengSR commented Feb 23, 2024

Because JSON documents can be arbitrarily large, we don't enable automatic change detection on them (more precisely, we don't define a value comparer that does value comparison). If we did, that means that would mean that we would do a snapshot of every JSON document loaded from the database, and a deep comparison against the snapshot once SaveChanges is called. This would be extremely bad for perf.

You can manually flag the JSON document as modified yourself:

ctx.Entry(designHubProject).Property(b => b.SectionStatuses).IsModified = true;

Note that your last snippet has designHubProject.ExecutiveSummarySectionStatus = ProjectSectionStage.COMPLETE, but ExecutiveSummarySectionStatus seems to be a property on SectionStatuses.

That's retarded tbh and completely not in line with how rest of EF Core works and completely unexpected behavior. What's the proper way to get it handled?

And no, changing the state to modifed is NOT the proper way to solve it, EF Core is behind a repository and the code/Entity containing it doesn't have any knowledge of EF Core internals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants