How to delete an object by id with entity framework

后端 未结 9 1890
你的背包
你的背包 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:55

    The same as @Nix with a small change to be strongly typed:

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

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

提交回复
热议问题