Entity Framework 4.1: How do I delete by object Id

后端 未结 4 1578
梦毁少年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条回答
  • 2021-02-01 16:16

    I use the following for my deletes, works great.

    public virtual ActionResult Delete(int commentID)
    {
        var c = new Comment(){CommentID = commentID};
        db.Entry(c).State= EntityState.Deleted;
        db.SaveChanges();
        return RedirectToAction(MVC.Blog.AdminComment.Index());
    }
    

    enter image description here

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-01 16:22

    The following code works well for me:

    var c = db.Set<T>().Find(id);
    db.Entry(c).State = EntityState.Deleted;
    db.SaveChanges();
    

    If this object wasn't tracked previously by the DbContext then it would hit the database, otherwise it would find it in memory.

    0 讨论(0)
  • 2021-02-01 16:23

    Just to convince you that your first code snippet must work here is a simple example you can copy, paste and test:

    • Create a new console application project (.NET 4)
    • Add reference to EntityFramework.dll (EF 4.1)
    • Delete the content of Program.cs and paste in the following:

    ->

    using System.Data.Entity;
    
    namespace EFDeleteTest
    {
        public class Car
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    
        public class MyContext : DbContext
        {
            public DbSet<Car> Cars { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                // Database with name "EFDeleteTest.MyContext"
                // will be created automatically in SQL Server Express
    
                int carId = 0;
                using (var context = new MyContext())
                {
                    var car = new Car { Name = "Test" };
                    context.Cars.Add(car);
                    context.SaveChanges();
    
                    carId = car.Id;
                }
                //Make breakpoint here and check that the car is indeed in the DB
    
                using (var context = new MyContext())
                {
                    var car = new Car() { Id = carId };
                    context.Cars.Attach(car);
                    context.Cars.Remove(car);
                    context.SaveChanges();
                }
                //Make breakpoint here and check that the car
                //indeed has been deleted from DB
            }
        }
    }
    

    If you have a SQL Server Express DB it will automatically create the DB.

    Perhaps this might help you to compare with your code because it looks as if something not visible in your code fragments is causing the different behaviour you describe.

    0 讨论(0)
提交回复
热议问题