Blake Rivell
Blake Rivell

Reputation: 13875

Updating records with Entity Framework Core 1.0 without needing to Update/UpdateRange

In Entity Framework Core 1.0 when I do something like:

var products = _context.Products.ToList();
foreach (var prod in products) 
{
     prod.Status = 1;
}
_context.SaveChanges();

I believe it will automatically update all products in the list to have a Status of 1.

If this is the case what happened to AddRange, UpdateRange, Add, Update, Remove, etc...? Do I even need to use these anymore? I feel like I don't need the update functions since the entities are tracked as soon as I pull them from the db.

Can someone please clear this up for me? My code is all over the place. In most cases I am using the UpdateRange and Update functions, but just recently noticed that I might just simply need to call _context.SaveChanges().

Now if I do something like this:

var products = _context.Products.AsNoTracking().ToList();
foreach (var prod in products) 
{
     prod.Status = 1;
}
// I believe I would have to UpdateRange here...
_context.UpdateRange(products);
_context.SaveChanges();

Upvotes: 1

Views: 9280

Answers (1)

Aida Bigonah
Aida Bigonah

Reputation: 197

Entity Framework can be in one of five states as defined by the EntityState enumeration. These states are:

Added: the entity is being tracked by the context but does not yet exist in the database Unchanged: the entity is being tracked by the context and exists in the database, and its property values have not changed from the values in the database Modified: the entity is being tracked by the context and exists in the database, and some or all of its property values have been modified Deleted: the entity is being tracked by the context and exists in the database, but has been marked for deletion from the database the next time SaveChanges is called Detached: the entity is not being tracked by the context

SaveChanges does different things for entities in different states:

Unchanged entities are not touched by SaveChanges. Updates are not sent to the database for entities in the Unchanged state. Added entities are inserted into the database and then become Unchanged when SaveChanges returns. Modified entities are updated in the database and then become Unchanged when SaveChanges returns. Deleted entities are deleted from the database and are then detached from the context.

when you change the state to Modified all the properties of the entity will be marked as modified and all the property values will be sent to the database when SaveChanges is called.

some examples for DbSet.AddRange,DbSet.RemoveRange, you can use : DbSet.AddRange adds collection(IEnumerable) of entities to the DbContext, so you don't have to add each entity individually.for example :

IList<Student> newStudents = new List<Student>();
newStudents.Add(new Student() { StudentName = "Student1 by addrange" });
newStudents.Add(new Student() { StudentName = "Student2 by addrange" });
newStudents.Add(new Student() { StudentName = "Student3 by addrange" });

using (var context = new SchoolDBEntities())
{
    context.Students.AddRange(newStudents);
    context.SaveChanges();
}

DbSet.RemoveRange is used to remove collection of entities :

List<Student> existingStudents = …..

using (var context = new SchoolDBEntities())
{
    context.Students.RemoveRange(existingStudents);
        context.SaveChanges();
}

note: AddRange() performance is better in the 12 range

Upvotes: 2

Related Questions