Reputation: 2050
I have this Code:
public static void SaveItem(Item itemFrom)
{
using (myEntitites ctx = new myEntitites())
{
Item itemTo = ctx.Items.First(x => x.ID = itemFrom.ID);
itemTo.Property1 = itemFrom.Property1;
itemTo.Property2 = itemFrom.Property2;
itemTo.Property3 = itemFrom.Property3;
//..lot of properties
ctx.SaveChanges();
}
}
I'm wondering whether there is a way to update an item without assigning each property.
itemFrom
is an updated version of itemTo
.
Upvotes: 0
Views: 41
Reputation: 33216
You can manually attach the item to the context without getting an object from the database. Entity Framework will update the correct row by using the primary key defined in the model.
public static void SaveItem(Item itemFrom)
{
using (myEntitites ctx = new myEntitites())
{
ctx.Items.Attach(itemFrom);
ctx.Entry(itemFrom).State = EntityState.Modified;
ctx.SaveChanges();
}
}
Upvotes: 2