How to delete an object by id with entity framework

后端 未结 9 1888
你的背包
你的背包 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 05:02

    In Entity Framework 6 the delete action is Remove. Here is an example

    Customer customer = new Customer () { Id = id };
    context.Customers.Attach(customer);
    context.Customers.Remove(customer);
    context.SaveChanges();
    

提交回复
热议问题