I have Table like the following image:
how can I delete all records of table using Entity FrameWork based on ProjectId ?
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.
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.
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.
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");
This one liner will do it:
db.ProRel.RemoveRange(db.ProRel.Where(c => c.ProjectId == Project_id));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With