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;
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;