unit DemoMainFormUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.Grids, Vcl.ComCtrls,
EntityDAC.Entity,
EntityDAC.EntityConnection,
EntityDAC.EntityContext,
EntityDAC.DataProvider.UniDAC,
SQLiteUniProvider,
DemoClasses;
type
TDemoMainForm = class(TForm)
lbDepts: TListBox;
lvEmps: TListView;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure lbDeptsClick(Sender: TObject);
private
FConnection: TEntityConnection;
FContext: TEntityContext;
FDepts: IEntityEnumerable<TDept>;
public
procedure Connect;
procedure PopulateDepts;
procedure PopulateEmps;
end;
var
DemoMainForm: TDemoMainForm;
implementation
{$R *.dfm}
{ TDemoMainForm }
procedure TDemoMainForm.FormCreate(Sender: TObject);
begin
FConnection := TEntityConnection.Create;
FContext := TEntityContext.Create;
FContext.Connection := FConnection;
FContext.ModelName := 'Demo';
end;
procedure TDemoMainForm.FormDestroy(Sender: TObject);
begin
FConnection.Free;
FContext.Free;
end;
procedure TDemoMainForm.FormShow(Sender: TObject);
begin
Connect;
PopulateDepts;
end;
procedure TDemoMainForm.lbDeptsClick(Sender: TObject);
begin
PopulateEmps;
end;
procedure TDemoMainForm.Connect;
begin
FConnection.ProviderName := 'UniDAC';
FConnection.DialectName := 'SQLite';
FConnection.ConnectionString := 'ProviderName=SQLite;Direct=True;DataBase=' + ExtractFilePath(Application.ExeName) + 'demo.db3';
FConnection.Connect;
end;
procedure TDemoMainForm.PopulateDepts;
var
Dept: TDept;
begin
FDepts := FContext.GetEntities<TDept>;
for Dept in FDepts do
lbDepts.Items.Add(Dept.Dname);
end;
procedure TDemoMainForm.PopulateEmps;
var
Emps: IEntityEnumerable<TEmp>;
Emp: TEmp;
Item: TListItem;
begin
lvEmps.Items.Clear;
if lbDepts.ItemIndex < 0 then
Exit;
Emps := FDepts[lbDepts.ItemIndex].Emps;
for Emp in Emps do begin
Item := lvEmps.Items.Add;
Item.Caption := Emp.Ename;
Item.SubItems.Add(Emp.Job);
end;
end;
end.