How to Create Temporary Entity Containing Collection?

Discussion of open issues, suggestions and bugs regarding EntityDAC
Post Reply
Luthfi
Posts: 6
Joined: Fri 09 Jan 2015 13:39

How to Create Temporary Entity Containing Collection?

Post by Luthfi » Fri 09 Jan 2015 13:50

I am trying to create a temporary entity which containing TCollectionData Collection(s) properties. If they weren't attached to any context, accessing these properties will generate errors. However if I attached them to a context, cancelling them (calling xxx.Cancel) will also error and another try to instantiate the same class with the same method will generate error which I believe caused by key value clashing in the cache.

What is the proper method to instantiate this kind of entity? Note that the created entity might get canceled. I mean not getting saved.

Edit1: Adding code snippet
Edit2: Additional note

Additional note:
I have used similar approach (shown in the code snippet) with entities that have no collection property, and it works well.

Here is example of code I used to create temporary entity:

Code: Select all

function TfChangeRecordList.CreateNewEntity(
  out AEntity: TMappedEntity): Boolean;
begin
  Result := False;
  with TfChangeRecord.Create(Self) do
  try
    ChangeRecord := Sys.EntityContext.CreateAttachedEntity<TChangeRecord>;
    if ShowModal=mrOk then
    begin
      AEntity := ChangeRecord;
      Result := True;
    end
    else begin
      // The following line is commented out since it generates error. It should not.
      // Will address the real problem later for now will have to accept the possible memory leak
      //ChangeRecord.Cancel;
    end;
  finally
    Free;
  end;
end;
And here is the class definition:

Code: Select all

  [Table('dbo.CH_ClientApproval')]
  [Model('SedeaDataModel')]
  [Key('FClientApprovalId')]
  TClientApproval = class(TMappedEntity)
  private
    [Column('ClientApprovalID')]
    [Generator(gtCustom, gfOnInsert)]
    FClientApprovalId: Integer;
    [Column('ChangeNumber')]
    FChangeNumber: Integer;
    [Column('ApproverID')]
    FApproverId: Integer;
    [Column('RequestStamp', [CanBeNull])]
    FRequestStamp: TDateTimeNullable;
    [Column('ApprovalStamp', [CanBeNull])]
    FApprovalStamp: TDateTimeNullable;

    [Column]
    [Reference('TChangeApprover', 'FClientApprovals', 'FApproverId', 'FApproverId', srNone, drNone)]
    FApprover: TReferenceData;
    [Column]
    [Reference('TChangeRecord', 'FClientApprovals', 'FChangeNumber', 'FChangeNumber', srNone, drNone)]
    FChangeRecord: TReferenceData;

    function GetClientApprovalId: Integer;
    procedure SetClientApprovalId(const Value: Integer);
    function GetChangeNumber: Integer;
    procedure SetChangeNumber(const Value: Integer);
    function GetApproverId: Integer;
    procedure SetApproverId(const Value: Integer);
    function GetRequestStamp: TDateTimeNullable;
    procedure SetRequestStamp(const Value: TDateTimeNullable);
    function GetApprovalStamp: TDateTimeNullable;
    procedure SetApprovalStamp(const Value: TDateTimeNullable);

    function GetApprover: TChangeApprover;
    procedure SetApprover(const Value: TChangeApprover);
    function GetChangeRecord: TChangeRecord;
    procedure SetChangeRecord(const Value: TChangeRecord);

  protected
    procedure Register; override;

    constructor Create(AMetaType: TMetaType); overload; override;

  public
    constructor Create; overload; override;

    property ClientApprovalId: Integer read GetClientApprovalId write SetClientApprovalId;
    property ChangeNumber: Integer read GetChangeNumber write SetChangeNumber;
    property ApproverId: Integer read GetApproverId write SetApproverId;
    property RequestStamp: TDateTimeNullable read GetRequestStamp write SetRequestStamp;
    property ApprovalStamp: TDateTimeNullable read GetApprovalStamp write SetApprovalStamp;

    property Approver: TChangeApprover read GetApprover write SetApprover;
    property ChangeRecord: TChangeRecord read GetChangeRecord write SetChangeRecord;
  end;

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: How to Create Temporary Entity Containing Collection?

Post by AlexP » Mon 12 Jan 2015 09:02

Hello,

Please provide the complete project including the model file to support*devart*com, in order for us to be able to reproduce the problem.

Luthfi
Posts: 6
Joined: Fri 09 Jan 2015 13:39

Re: How to Create Temporary Entity Containing Collection?

Post by Luthfi » Wed 14 Jan 2015 08:32

Hi Alex,

I just sent an email with demo project to support devart com as per suggestion. Do I need to also post the demo project an issues description here? I just really need quick solution here.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: How to Create Temporary Entity Containing Collection?

Post by AlexP » Thu 15 Jan 2015 10:17

Thank you. We have received your sample. We will try to respond as soon as possible.

Post Reply