Reputation: 24572
I have this method:
[Route("Delete")]
public IHttpActionResult Delete()
{
}
What I would like to do is to delete from three tables that are here:
public System.Data.Entity.DbSet<SampleSentence> SampleSentences { get; set; }
public System.Data.Entity.DbSet<Synonym> Synonyms { get; set; }
public System.Data.Entity.DbSet<WordForm> WordForms { get; set; }
Is there a delete function in EF6 that I can use to delete all the rows or should I somehow make a SQL call?
Upvotes: 5
Views: 686
Reputation: 10115
I just found on stackoverflow
db.ProRel.RemoveRange(db.ProRel.Where(c => c.ProjectId == Project_id));
How to delete multiple records with Entity Framework ASP.Net MVC 5?
But I use this method to delete a row
var Cert = (from cert in db.TblCompCertUploads where cert.CertID == ID select cert).FirstOrDefault();
if (Cert != null)
{
db.TblCompCertUploads.DeleteObject(Cert);
db.SaveChanges();
ViewBag.Msg = "Deleted Successfully";
}
For Mulitple Rows
List<tbl_CompCertificate> tbp = db.tbl_CompCertificate.Where(t => t.CompCer_CompId == CompId).ToList();
foreach (tbl_CompCertificate t in tbp)
{
db.tbl_CompCertificate.DeleteObject(t);
db.SaveChanges();
}
Upvotes: 1
Reputation: 13488
You can use this EntityFramework.Extended library, with it help one can write:
context.SampleSentences.Delete();
context.Synonyms.Delete();
context.WordForms.Delete();
Upvotes: 2
Reputation: 247123
Assuming you have a DbContext derived class...
using (var transactionScope = new TransactionScope()) {
try {
string sql = @"
DELETE SampleSentence
DELETE Synonym
DELETE WordForm
";
int count = myDbContext.Database.ExecuteSqlCommand(sql);
if(count > 0)
transactionScope.Complete();
} catch(Exception ex) {
//Logging
}
}
Upvotes: 2