EntityDAC

TCustomEntityContext.CreateAttachedEntity Method

The method is designed for creating a new entity instance and simultaneous attaching it to the data context

Class

TCustomEntityContext

Syntax

function CreateAttachedEntity(MetaType: TMappedMetaType): TMappedEntity; overload;

Parameters
MetaType
The meta-type of the entity to be created.

Remarks

The method creates a new entity instance of the specified meta-type. The entity attributes forming the primary key will be initialized by the default values. The entity instance created by this method is initially attached to the data context and placed to the object cache (in contrast to the TCustomEntityContext.CreateEntity method), therefore you can already perform modification operations for the instance: TCustomEntityContext.Delete, TCustomEntityContext.Save, TCustomEntityContext.Cancel, without pre-calling TCustomEntityContext.Attach.

Note, that when using this method, the entity attributes forming the primary key are initialized by the default values, therefore it may cause the exception when attaching the entity to the data context due to unique key violation.

Example

var
  EmpType: TMappedMetaType;
  EmpEntity: TEntity;
begin
  EmpType := Context.Model.MetaTypes.Get('Emp');
  EmpEntity := Context.CreateAttachedEntity(EmpType);
  // ...
end; 

See Also

© 1997-2017 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback