0
private void btnLoadPriceList_Click(object sender, EventArgs e)
{

        using (PriceUpdaterSQLEntities1 context = new PriceUpdaterSQLEntities1())
        {
            context.PRICE_LIST.RemoveRange(context.PRICE_LIST.Where(x => x.PRICE_ID != null));
            context.SaveChanges();
        }
}

I'm new to Entity framework and would like to know why my delete operation is not being updated to my SQL table in localDB?

I need to delete every record in the PRICE_LIST table before refilling it with new records. PRICE_ID is the PK and cannot be null, which is why I queried for not null. The code runs without errors, but nothing in the table is actually deleted. I've done a lot of reading and don't understand what I'm doing wrong here. I've rebuilt the Entity Model already, so I'm sure its not the connection string.

Ian
  • 105
  • 9
  • Are you sure `RemoveRange()` is returning any items? Have you debugged it to see if anything is matching your query? – maccettura Jul 14 '17 at 18:19
  • Please check this https://stackoverflow.com/questions/2519866/how-do-i-delete-multiple-rows-in-entity-framework-without-foreach – Ankit Rana Jul 14 '17 at 18:34
  • I have already read question 2519866 before posting my question. That is where I found RemoveRange(). I changed the code to (x => x.PRICE_ID == "001") to test if I could remove one record and it didn't delete price_id 001. So it would appear that the RemoveRange() isn't pulling anything. – Ian Jul 14 '17 at 19:05
  • I suggest overriding your dbcontext's `Log` property, and log everthing sent to the database to your debug window. Make sure its doing what you think its doing. Alternatively you can use SQL Profiler if you're running it against a database you have admin rights on. –  Jul 14 '17 at 20:42
  • I went a different route and tried the ExecuteSqlCommand("Delete From Price_List"). The log is below, but the table still has all its data. Opened connection at 7/14/2017 2:04:03 PM -07:00 Started transaction at 7/14/2017 2:04:03 PM -07:00 DELETE FROM PRICE_LIST -- Executing at 7/14/2017 2:04:03 PM -07:00 -- Completed in 92 ms with result: 1457 Committed transaction at 7/14/2017 2:04:03 PM -07:00 Closed connection at 7/14/2017 2:04:03 PM -07:00 – Ian Jul 14 '17 at 21:06
  • Then you are not running against the database you think you are. –  Jul 14 '17 at 22:24
  • I created another project from scratch and the RemoveRange() ran, but seemed very slow. I don't think this is the best way to delete 2,000+ records. – Ian Jul 15 '17 at 02:04

1 Answers1

0

I just went with the old SqlConnection and SqlCommand objects to get things working. Problem solved.

Ian
  • 105
  • 9