Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete multiple records with Entity Framework ASP.Net MVC 5? [closed]

I have Table like the following image:

enter image description here

how can I delete all records of table using Entity FrameWork based on ProjectId ?

like image 701
Benafsh Yalda Avatar asked Nov 28 '14 16:11

Benafsh Yalda


People also ask

How delete multiple records from database in MVC?

Choose your data connection then select your database then click Next then select Tables then enter the Model namespace then click Finish. Add a Controller or use the default Home controller. I used the default Home controller. Add a new action into your controller to delete rows.

How can I delete multiple records using checkbox in ASP NET MVC?

Select Web from the left panel, choose ASP.NET Web Application, give a meaningful name to your project, then click on OK as shown in below screenshot. After clicking on OK one more window will appear; choose Empty, check on MVC checkbox and click on OK as shown below screenshot.

How do I delete a record in Entity Framework?

Delete a Record In Connected Scenario, you can use the Remove or RemoveRange method to mark the record as Deleted . In Disconnected Scenario, you can attach it to the context and set its state as Deleted . Calling SaveChanges will send the delete query to the database.

How do I delete all records from a table in Entity Framework?

The Other quickest simple option to delete all the rows in a table in entity framework is to use the TRUNCATE table query and execute it with the ExecuteSqlCommand as shown below. dbContext. Database. ExecuteSqlCommand("TRUNCATE TABLE Customer");


1 Answers

This one liner will do it:

  db.ProRel.RemoveRange(db.ProRel.Where(c => c.ProjectId == Project_id)); 
like image 157
MJ X Avatar answered Oct 17 '22 16:10

MJ X