Lazy<TMemoryStream> cannot update

Issue #291 closed
Pavel Novak created an issue

I have one Entity:

[Entity]
  [Table('SablonaEtiketa', 'Obch_Data')]
  TMSablonaEtiketa = class
  private
   ...
    [Column('Sablona', [], 50, 0, 0, 'Šablona')]
    FLazySablona: Lazy<TMemoryStream>;
    function GetSablona: TMemoryStream;
    procedure SetSablona(const Value: TMemoryStream);
  public
   ...
    property LazySablona: Lazy<TMemoryStream> read FLazySablona write FLazySablona;
    property Sablona: TMemoryStream read GetSablona write SetSablona;
  end;

function TMSablonaEtiketa.GetSablona: TMemoryStream;
begin
  Result := FLazySablona;
end;

procedure TMSablonaEtiketa.SetSablona(const Value: TMemoryStream);
begin
  if Value <> nil then
    FLazySablona.Value.LoadFromStream(Value)
  else
    FLazySablona.Value.Clear;
end;

When I create new Entity and call Repository.Save(entity) stream is saved to DB.

But when I modify only stream in this Entity and call Repository.Save(entity),entity is not updated.

In EqualsRec2Rec is allways equal (ILazy<TMemoryStream>=ILazy<TMemoryStream>) and TEntityMap.GetChangedMembers returns nothing and no SQL for UPDATE.

Comments (3)

  1. Stefan Glienke repo owner

    What you are doing here is fetching the lazy value from the factory and then call its LoadFromStream. Since the internal object created by the lazy is still the same the EqualsRec2Rec will return True - it could not even look inside because it's a reference type, the TMemoryStream inside of it is still the same instance and it did not store its content. You can only solve this by creation of a new internal value by re-assigning its value.

    The following should work:

    procedure TMSablonaEtiketa.SetSablona(const Value: TMemoryStream);
    var
      Stream: TMemoryStream;
    begin
      Stream := FLazySablona;
      if Value <> nil then
        Stream.LoadFromStream(Value)
      else
        Stream.Clear;
      FLazySablona := Stream;
    end;
    

    This will reuse the already created TMemoryStream instance but force a recreation of the ILazy<TMemoryStream> reference causing the EqualsRec2Rec to return true.

  2. Log in to comment