Is it possible to get LINQ to SQL to delete a record using the PK, without loading the record first? Similar to NHibernate's proxy object functionality?
Proper way to delete record in LINQ to Entities My delete function worked when I did the following:... using (var ctx = new MyEntity()) { var x = (from y in ctx. Employees orderby y. EmployeeId descending select y).
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.
Deleting an entity is done using the Remove or RemoveRange method of the DbSet. Alternatively, you can also set the entity state as Deleted . We can delete records either in connected or disconnected Scenarios. We will also look at how to remove multiple records from the database using the RemoveRange method.
You should be able to do it this way:
var person = new Person();
person.ID = someID;
using (var context = new DataContext(connString))
{
context.Persons.Attach(person, false); //attach is as unmodified
context.Persons.DeleteOnSubmit(person); //remove it
context.SubmitChanges(); //submit changes to db
}
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