Tracking changes in json column in EF Core 8 and postgres

108 Views Asked by At

i have a problem with tracking changes in json column in EF Core 8 and postgres. It seems like explicity setting entries as modified does not work

Models i am using:

public class DocumentReadModelEntity<T> where T : IDocumentReadModel
{
    public Guid Id { get; set; }
    public T Data { get; set; }

    public DocumentReadModelEntity()
    {
        
    }

    public DocumentReadModelEntity(T data)
    {
        Data = data;
        Id = data.Id;
    }
}
public sealed class SupplierReadModel : IDocumentReadModel
{
    public string? Description { get; set; }
    public string? Website { get; set; }
    public Guid? LogoFileId { get; set; }
    public bool IsActive { get; set; }
    public bool ShouldSendRemittanceEmail { get; set; }

    public List<SupplierBlockModel> Blocks { get; set; }
    public List<EstateBlockModel> Estates { get; set; }

    public Guid CompanyId { get; set; }
    public CompanyShortData? CompanyData { get; set; }

    public BankAccountModel? BankAccount { get; set; }
}

every complex property in SupplierReadModel (in this example they are: SupplierBlockModel, EstateBlockModel, CompanyShortData, BankAccountModel) inherits from

[Owned]
public abstract class ReadModelDataModel
{
}

For example (SupplierBlockModel):

public class SupplierBlockModel : ReadModelDataModel
{
    public Guid BlockId { get; set; }
    public BlockShortData? BlockData { get; set; }
}

Nested properties inherit as well

public class BlockShortData : ReadModelDataModel
{
    public string CustomId { get; set; }
    public string BlockName { get; set; }
    [JsonIgnore]
    public string FullName => $"[{CustomId}] {BlockName}";
}

Entity configuration looks like:

public static void ApplyDefaultConfiguration<T>(this EntityTypeBuilder<DocumentReadModelEntity<T>> builder) where T : class, IDocumentReadModel
{
    builder.ToTable(typeof(T).Name);

    builder.HasKey(x => x.Id);

    builder.OwnsOne(x => x.Data, x =>
    {
        x.ToJson();
    });
}

I am quering db context as:

 return await _entities.AsNoTracking()
     .Where(x => ids.Contains(x.Id))
     .Select(x => x.Data)
     .ToListAsync();

where _entities is _entities = _dbContext.Set<DocumentReadModelEntity<T>>(); in generic repository public class DocumentStore<T> : IDocumentStore<T> where T : class, IDocumentReadModel, new()

when i am updating SupplierReadModel and i try to update this entry with:

public void Update(T updated)
{
    var entity = new DocumentReadModelEntity<T>(updated);
    _entities.Entry(entity).State = EntityState.Modified;
}

and then

public async Task Save()
{
   await _dbContext!.SaveChangesAsync();
}

nothing is saved. additionaly when i look at the debug view of db context change tracker i can see

DocumentReadModelEntity<SupplierReadModel> {Id: 018d834f-75d9-48eb-8af1-543579976bc5} Modified
    Id: '018d834f-75d9-48eb-8af1-543579976bc5' PK
  Data: <not found>

what do i wrong?

0

There are 0 best solutions below