EntityDAC

TCustomEntityContext.CreateEntity Method

The method is designed for creating a new entity instance.

Class

TCustomEntityContext

Syntax

function CreateEntity(MetaExpr: IMetaType; const KeyValues: array of Variant): TMappedEntity; overload;

Parameters
MetaExpr
The meta-type of the entity to be created.
KeyValues
The array of initial values of meta attributes forming the entity primary key.

Remarks

The method creates a new entity instance of the specified meta-type. The attributes forming the entity primary key will be initialized by the specified values. The entity instance created by this method is not attached to the data context (is not placed to the object cache), therefore it won't be automatically destroyed on application shutdown. It should be destroyed manually. It is expedient to use the method in case when the primary key value of an entity is unknown at the moment of its creation (or there is no confidence in its uniqueness), in order to avoid an exception on an attempt to place an object to the cache.

To attach an entity instance to the data context and place it to the object cache (in order to perform further modification operations: TCustomEntityContext.Delete, TCustomEntityContext.Save, TCustomEntityContext.Cancel), the TCustomEntityContext.Attach method should be used. To create an entity instance with simultaneous attaching it to the data context, the TCustomEntityContext.CreateAttachedEntity method should be used.

Example

var
  EmpType: IMetaType;
  EmpEntity: TEntity;
begin
  EmpType := Context['Emp'];
  EmpEntity := Context.CreateEntity(EmpType, [1, 1]);
  // ...
end; 
© 1997-2017 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback