Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Deleting a list of objects in Entity Framework

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.51k
    Comment on it

    Entity framework provides numerous ways to batch delete the objects from Database. Below are the step for way 1 :

    1. Load objects that has to be deleted in memory.
    2. Remove the objects from context

    3. And then save these changes. This statement will reflect the actual database changes.
    var users = context.Users.Where(u => u.Name == "test");
     foreach (var u in users)
        {
          context.Users.Remove(u);
        }
    context.SaveChanges();
    

    second way for batch delete :

    context.Database.SqlQuery<User>(
      "DELETE FROM Users WHERE Name = @name",
      new [] { new SqlParameter("@name", "John") }
    );
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: