Database Reference
In-Depth Information
static void DisplayLineItems()
{
bool found = false;
using (var context = new EF6RecipesContext())
{
foreach (var lineitem in context.LineItems)
{
Console.WriteLine("Line item: Cost {0}",
lineitem.Cost.ToString("C"));
found = true;
}
}
if (!found)
Console.WriteLine("No line items found!");
}
Following is the output of the code in Listing 7-5:
Original set of line items...
Line item: Cost $99.29
Line item: Cost $29.95
Line item: Cost $109.95
Line item: Cost $49.95
After removing a line item from an invoice...
Line item: Cost $29.95
Line item: Cost $109.95
Line item: Cost $49.95
After removing an invoice...
Line item: Cost $29.95
After removing a line item...
Line item: Cost $49.95
After updating a line item...
Line item: Cost $39.95
How It Works
The code in Listing 7-5 deletes line items in three ways. First it deletes a line item from an invoice's collection. Because
a line item is dependent on the invoice for its identity, Entity Framework marks the referenced line item for deletion.
Next it deletes an invoice. Entity Framework marks all of the dependent line items for deletion. Finally, the code
deletes the last remaining line item directly by calling Remove() on the context's LineItems entity set.
You can modify all of the properties of a dependent entity except for properties that participate in the identifying
relationship. In our model, we can modify the Cost property in a line item, but we can't change the Invoice navigation
property.
When a principal object in an identifying relationship is saved to the database, the key that is generated at the
database (for store-generated values) is written to the principal entity and to all of its dependent entities. This ensures
that all are synchronized in the database context.
 
Search WWH ::




Custom Search