Delete an item by Primary Key

Discussion of open issues, suggestions and bugs regarding EntityDAC
Post Reply
elion
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Delete an item by Primary Key

Post by elion » Wed 10 Dec 2014 07:30

Hi there!

How can I delete an item explicit by an ID?

Now, I have first to retrieve an item from the database (e.g. SomeContext.GetEntity<TSomeEntity>), before I can execute DeleteAndSave, is it right?

Thank you!

I would like to have a functionality like shown below:

Code: Select all

var
  SomeEntity: TSomeEntity;
  SomeContext: TEntityContext;
begin
  SomeEntity := TSomeEntity.Create;
  SomeEntity.Id = 1234; // <-- this is a primary key

  SomeContext.Delete(SomeEntity);
  SomeContext.Save(SomeEntity);  
end;

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Delete an item by Primary Key

Post by AlexP » Wed 10 Dec 2014 09:13

For record deletion, you can use the following code:

Code: Select all

var
  SomeEntity: TSomeEntity;
  SomeContext: TEntityContext;
begin
  SomeEntity := SomeContext.GetEntity<T>(1234)
  SomeContext.DeleteAndSave(SomeEntity);
end;

elion
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Re: Delete an item by Primary Key

Post by elion » Wed 10 Dec 2014 09:28

AlexP wrote:For record deletion, you can use the following code:

Code: Select all

var
  SomeEntity: TSomeEntity;
  SomeContext: TEntityContext;
begin
  SomeEntity := SomeContext.GetEntity<T>(1234)
  SomeContext.DeleteAndSave(SomeEntity);
end;
But in this case, I have to execute sql in order to get the item. But if I know the ID, I could delete the item directly without executing sql before.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Delete an item by Primary Key

Post by AlexP » Thu 11 Dec 2014 10:15

To delete record, you can execute the following SQL query in the ExecuteSQL method:

Code: Select all

EntityConnection.ExecuteSQL('DELETE FROM TABLE WHERE FIELD = 123));

elion
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Re: Delete an item by Primary Key

Post by elion » Thu 11 Dec 2014 13:49

I see... :shock: Thx for your reply! :)

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Delete an item by Primary Key

Post by AlexP » Fri 12 Dec 2014 06:47

If you have any further questions, feel free to contact us.

Post Reply