Creating New Application That Shows Entity List

In this part of the tutorial, we start to create our first application using EntityDAC.

Step 1

In RAD Studio, let's create a new VCL Forms Application project, and set its main form name to "DemoMainForm". Add a new data module to the application and name it "DemoDataModule". Then add to the project previously generated model units (DemoModel.Context.pas, DemoModel.Linq.pas, DemoModel.MetaData.pas, DemoModel.Classes.pas).

Save the project as "DemoProject", naming the main form and data module units as "DemoMainFormUnit" and "DemoDataModuleUnit" respectively.

Open the project source (select the "View Source" item in the project context menu; or select the project in the Project Manager and press Ctrl+V). In the project source, change the order of the project's form creation:

move the line

Application.CreateForm(TDemoDataModule, DemoDataModule);

above the line

Application.CreateForm(TDemoMainForm, DemoMainForm);

Then, save the project.

Step 2

Open the data module in the Form Designer and place TEntityConnection, TEntityXMLModel and TEntityContext components onto it from the "EntityDAC" component palette.

In addition, if you are using UniDAC, place TUniDACDataProvider from the "EntityDAC Providers" component palette and TSQLiteUniProvider from the "UniDAC Providers" component palette. Or, if you are using LiteDAC - place the TLitedacDataProvider component from the "EntityDAC Providers" component palette.

Step 3

In order to have access to model's metadata in design-time, set the FileName property of the TEntityXMLModel component to the *.Mapping.xml file.

Then select the EntityConnection component, specify the path to the database in the ConnectionString property and set other properties as follows:

Finally, set up the data context properties.

Step 4

Let's set up the main form in order to display a list of database entities.

Add the DemoDataModuleUnit into the USES clause of the main form unit. Then open the main form in the form designer and place TEntityDataSet, TDataSource and TDBGrid components onto it. Set the component names as it is shown on the picture. Set the DataSource property of the TDBGrid component to "MasterSource", and the DataSet property of the TDataSource component – to "MasterDataSet". Also, place TButton on the form and set its name to "Button1".

TEntityDataSet is designed to store an arbitrary list of entities, thus, it can be set up in run-time only. Write the code shown below in the Button1.OnClick event handler.

{$R *.dfm}


procedure TDemoMainForm.Button1Click(Sender: TObject);
  Depts: ILinqQueryable;
  Depts := Linq.From(DemoDataModule.EntityContext1['Dept']).Select;
  MasterDataSet.SourceCollection := DemoDataModule.EntityContext1.GetEntities(Depts);


In order to declare the Depts variable of type ILinqQueryable, add the EntityDAC.Enumerable and EntityDAC.Linq units into the USES clause.

In the code we initialize the Depts variable with the result of the LINQ query that returns the list of all entities of metatype 'Dept', then set Depts as the source collection for MasterDataSet.

Now we can make sure that everything is done correctly. Compile and run the project, and press the "Open" button.

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