Database Reference
In-Depth Information
Figure 8-7. A model with a single Item entity
To update the entity with new values after retrieving the entity and then to apply changes to save in database,
follow the pattern in Listing 8-9.
Listing 8-9. Retrieving the Newly Added Entity and Replacing Its Values Using the Entry() Method
class Program
{
static void Main(string[] args)
{
RunExample();
}
static void RunExample()
{
int itemId = 0;
using (var context = new EFRecipesEntities())
{
var item = new Item
{
Name = "Xcel Camping Tent",
UnitPrice = 99.95M
};
context.Items.Add(item);
context.SaveChanges();
// keep the item id for the next step
itemId = item.ItemId;
Console.WriteLine("Item: {0}, UnitPrice: {1}",
item.Name, item.UnitPrice.ToString("C"));
}
using (var context = new EFRecipesEntities())
{
// pretend this is the updated
// item we received with the new price
var item = new Item
 
Search WWH ::




Custom Search