Reputation: 13
I am using Entity Framework, C# 4.0 and Visual Studio 2010. I need to execure a simple SQL query to delete the content of four tables. The query contains:
DELETE FROM dbo.tblMFile
DELETE FROM dbo.tblMContacts
DELETE FROM dbo.tblPersonDetails
DELETE FROM dbo.tblAddresses
There is a foreign key contraint between some of the tables.
There seems to be no simple way to do this.
With reference to my comments on the first respondant's answer:
Because of the highly confidential nature of the data I NEED a way to quickly delete all content (a requirement and a security issues)
I am new to EF and keen to learn
I am using delete instead of truncate because of the foreign key constraints mentioned above (also the SQL above is illustrative, not definitive).
I am a firm and long time believer in strong typing and prefixes all my objects with a type indicator. It has saved me hours (or days even) of debugging.
Humans are polite, sensitive and informative (among many other attributes). I aspire to be human.
Upvotes: 1
Views: 1965
Reputation: 62137
No, and that is good so. ORM's are not for bulk deletions. If you really want to use LINQ to issue a DELETE, use BlToolkit that allows you to express arbitrary pretty much standard DML non bulk (no truncate) in LINQ.
Delete ALL records?
TRUNCATE TABLE, NOT DELETE.
TRUNCATE TABLE tblMFile
Depending how much the table contains that can be thousands of times faster. Mostly because it does also not log the deleted data - just the fact that a truncate occurred.
Upvotes: 3
Reputation: 866
Try This
_db.ExecuteStoreCommand("delete from MachineMaster where IdMachine =" + obj_Machine.IdMachine + " or IdMasterMachine=" + obj_Machine.IdMachine );
Upvotes: 0
Reputation: 4150
To add to the answer from @TomTom. You can't do that directly using Entity Framework. But you can:
ExecuteStoreQuery
see see and write the SQL command directlyUpvotes: 2