Delete query

Posts   
 
    
jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 28-Dec-2009 12:23:18   

Hello,

I would like to execute the following sql using Linq to LLBLGen.

DELETE FROM [Emp] WHERE ([Name] = @p0) AND ([Id] = @p1)

How can this be done?

Found this code at: http://blogs.msdn.com/wriju/archive/2007/07/17/linq-to-sql-delete-data-through-object-model.aspx

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 28-Dec-2009 14:02:48   

Linq2LLBLGen is for querying the database only. Action queries (INSERT, UPDATE & DELETE) should be carried out using using LLBLGen framework code.

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 28-Dec-2009 14:06:35   

Walaa wrote:

Linq2LLBLGen is for querying the database only. Action queries (INSERT, UPDATE & DELETE) should be carried out using using LLBLGen framework code.

Ok that makes sense. I've found the following example in the documentation:

CustomerEntity customer = new CustomerEntity("CHOPS");
customer.Delete();

For this object to be deleted it first needs to fetched. Is there a way I can delete the object without first fetching it (so it's faster) like in the sql example I posted above?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 28-Dec-2009 14:11:48   

Are you using SelfServicing? (Which automatically fetches the entity when the Ctor is called using a PK value)

Any way, you can always Delete entities directly from the database. Please use an entityCollection of the specified entity type, and call DeleteMulti(filter), and pass the appropriate filter.

Documentation wrote:

Deleting one or more entities from the persistent storage If you want to delete one or more entities from the persistent storage the same problem as with updating a set of entities appears: you first have to load them into memory, call Delete() and they'll be deleted. To delete a set of entities from the persistent storage directly, you can use DeleteMulti() overloads or the DeleteMultiManyToOne method to achieve your goal. All DeleteMulti() methods work directly on the persistent storage except one, the DeleteMulti() method which does not take any parameters. That one works with the objects inside the collection and deletes them one by one from the persistent storage using an own transaction if the current collection isn't part of an existing transaction. (See for more information about transactions the section Transactions). The DeleteMulti() methods which do accept parameters and thus work on the persistent storage work the same as the UpdateMulti() methods, except of course the DeleteMulti() methods do not accept an entity with changed fields. See for an example how to filter rows for DeleteMulti*() the UpdateMulti() example given earlier on this page.

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 28-Dec-2009 14:16:06   

Yes I'm using SelfServicing. Looks like DeleteMulti(filter) is exactly what I´m looking for. Thanks!

Suresh
User
Posts: 6
Joined: 19-Feb-2010
# Posted on: 19-Feb-2010 15:36:29   

Hi

How to perform this same delete operation for the table without primary key or without any unique column...

It is not working for me even with predicate expression passed................

Same code works fine for the table with Primary key.......

Is any thing I am missing,............

Please help me....

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Feb-2010 19:28:32   

Hi Suresh,

First of all, you can't delete an entity you don't have any PK. You can perform a DeleteMulti(filter) though. Still, is no good to have a table without PK. If you have problems please post more information (code snippet, LLBLGen version, runtime library version, etc.) http://llblgen.com/tinyforum/Messages.aspx?ThreadID=12769

David Elizondo | LLBLGen Support Team