How to delete an object by id with entity framework

后端 未结 9 1882
你的背包
你的背包 2020-11-29 04:05

It seems to me that I have to retrieve an object before I delete it with entity framework like below

var customer = context.Customers.First(c => c.Id == 1         


        
9条回答
  •  日久生厌
    2020-11-29 04:53

    If you dont want to query for it just create an entity, and then delete it.

    Customer customer  = new Customer() {  Id = 1   } ; 
    context.AttachTo("Customers", customer);
    context.DeleteObject(customer);
    context.Savechanges();
    

提交回复
热议问题