Delete an item by Primary Key

Delete an item by Primary Key

Postby 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;
elion
 
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Re: Delete an item by Primary Key

Postby 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;
AlexP
Devart Team
 
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Delete an item by Primary Key

Postby 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.
elion
 
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Re: Delete an item by Primary Key

Postby 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));
AlexP
Devart Team
 
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Delete an item by Primary Key

Postby elion » Thu 11 Dec 2014 13:49

I see... :shock: Thx for your reply! :)
elion
 
Posts: 29
Joined: Wed 10 Dec 2014 07:15

Re: Delete an item by Primary Key

Postby AlexP » Fri 12 Dec 2014 06:47

If you have any further questions, feel free to contact us.
AlexP
Devart Team
 
Posts: 5530
Joined: Tue 10 Aug 2010 11:35


Return to EntityDAC