Entity Framework 4.1: How do I delete by object Id

后端 未结 4 1579
梦毁少年i
梦毁少年i 2021-02-01 15:42

I would like to know how to delete an object from Entity Framework 4.1 without first having to load the object from the database. I have found these other 2 answers on Stack Ove

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 16:16

    use stub entities:

    public void DeleteCar(int carId)
    {
      var car = new Car() { Id = carId };
      _dbContext.AttachTo("Cars",car); 
      _dbContext.DeleteObject(car); 
      _dbContext.SaveChanges();
    }
    

    reference:

    http://blogs.msdn.com/b/alexj/archive/2009/06/19/tip-26-how-to-avoid-database-queries-using-stub-entities.aspx

提交回复
热议问题