Reputation: 1767
I have an entity called Person and another called Car where evey car can be owned by one person only. The person is retrieved from the DB via Ef. Then I am Setting the person as rhe datacontext to a WPF grid where the properties are bound to some controls. So fine so goood. Inside the grid i have an itemscontrols which is showing the CarCollestion by direct binding as the itemssource from the Person object. Presentatio works perfect. Iselecta car and edit it, the car object accepts the changes. On saving the person I would also like to save the car collection. I attach the person to a new context, change the object state of the person to modified and hit savechanges. Changes in person are stored in the Db but not the changes in cars. By attaching the modified car and setting the object state as well, the changes are saved to the db as well.
My question is, if it is possible to have only the person object attaching to the context , and the context would attach all car entities on its own. Save all child and subchild ovjects on its own.
One solution would be to have the object modified/added/ deleted, in a list stored and attach every single one of them after attaching the person to a new context.
Sorry for not providing any code, i am out of office.
Also, this is a simplified versio of the whole problem but accurate
Edit - some code added - finally in the office
<Grid Name="testGrid" DataContext="{Binding}" Initialized="testGrid_Initialized">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Name="ID" Grid.Row="0" Content="{Binding Path=ID}"/>
<TextBox Name="Name" Grid.Row="1" Text="{Binding Path=Name}"/>
<ItemsControl Grid.Row="2" ItemsSource="{Binding Path=Cars}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch">
</StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Name="btnCar" Content="{Binding Path=Name}" Click="btnCar_Click" Tag="{Binding .}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Content="Save" Click="Button_Click"/>
</Grid>
and the eventhandlers ....
private void btnCar_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
Car c = b.Tag as Car;
Car.Name += "-";
}
#endregion
private void testGrid_Initialized(object sender, EventArgs e)
{
using(var c = new DbContext())
{
globalPerson = c.Person.First();
Grid g = sender as Grid;
g.DataContext = globalPerson;
globalPerson.Name += "_";
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
using(var c = new DbContext())
{
c.Attach(globalPerson);
c.ObjectStateManager.ChangeObjectState(globalPerson, EntityState.Modified);
c.SaveChanges();
}
}
The result of this is that the after calling saveChenges The Person changes are saved in the DB, but not the ones that have been made to the name of the car. If I also attach the modified Car entity to the context they are then also saved. Everything works.
the problem is, I have to maintain a list of entities that have been modified, added, .... and manually attach everyone single of them. It gets messy.
I would like to know , Is it possible to have the Car entities automatically be attached to a context when the person is attached.
Maybe I am missing a property that takes care of it of the Entity.
Upvotes: 0
Views: 60