How to delete a db row in LINQPAD
Asked Answered
A

2

10

Total NOOB question. I have been using the new linqpad for about 20 minutes now. Great!

But now i want to delete a row in the db. I am using a EF5.0 connection. I cant seem to find anything in the help files or on the net. The only thing I can find is DeleteOnSubmit which doesnt work with EF (as far as I can see). I have also tried DeleteObject which doesnt work either. This is what I have tried.

var co = Companies.First();
co.Dump();

Companies.DeleteObject(co);
Apostate answered 7/9, 2013 at 13:14 Comment(3)
Shouldn't this be just DeleteObject(co) and then SaveChanges() ?Aleph
@Aleph - see the error below. I found another way to delete rows in the database because I don't have time to waste on this. I created a new connection directly to the database. My original effort was trying to delete entities through the context but I have not been able to get that to work.Apostate
You can only get that error if you use Companies.DeleteObject(co). The error should not occur if you call just call DeleteObject(co) (or this.DeleteObject(co) if you prefer).Aleph
S
15

This is old... and I don't know if/when this was added (probably in response to this exact scenario)… but you can accomplish this (in your given example) as follows:

//test the following line to ensure the context doesn't complain about the .First() reference

Companies.DeleteOnSubmit(Companies.First()); 

Companies.Context.SubmitChanges();
Supersensual answered 29/6, 2018 at 15:20 Comment(1)
using SubmitChanges() is enough, there is no need call Companies.Context.SubmitChanges(), this is in Linqpad 5.Dunaway
P
3

You need to SaveChanges on your context (Companies) for your row to be deleted.

Companies.SaveChanges();

Prepuce answered 7/9, 2013 at 13:28 Comment(2)
This error is being thrown:Cannot execute text selection: 'System.Data.Entity.DbSet<DAL.Entities.CompanyEntity>' does not contain a definition for 'DeleteObject' and no extension method 'DeleteObject' accepting a first argument of type 'System.Data.Entity.DbSet<DAL.Entities.CompanyEntity>' could be found (press F4 to add a using directive or assembly reference)Apostate
.. and linqpad doesn't explicitly expose the context.Apostate

© 2022 - 2024 — McMap. All rights reserved.