How to use DAL to update multiple tables in entity RRS feed

  • Question

  • I have a WPF master detail form with two DataGrids. Customers is the master and Addresses is the details. I am using Entity framework to query and save the tables.
    I want to be able to make a change to 1 Customer record and one or more of it's related Addresses in the DataGrids. Then I want SaveChanges() to commit all changes from both tables.
    As long as I perform all the work in my code behind, SaveChanges() commits all changes to both tables.
    The problem I am having is when I try to layer my data access with a Data Access Layer. The DAL returns the master detail query OK, but when the code behind tries to pass the modified entity back to the DAL, only the master table is seen as changed and only the master table gets saved.
    Here is the pseudo code of my WPF code behind and my DAL layer. What do I have to do to get the DAL to save changes to both tables?
    //WPF code behind
    //Code behind properties.
    public ObservableCollection<Customer> CustomerObj { get; set; }
    void Window_Loaded()
    CustomerObj = new ObservableCollection<Customer>(DAL.GetAllCustomers());
    private void Save_Click()
    //Get Current Customer record and pass it to DAL.
    new DAL().Save(CurrentRecord);
    private myEntities contextDB = new myEntities();
    public List<Customer> GetAllCustomers()
    return contextDB.Customers.Include("Address").OrderBy(x => x.CompanyName).ToList();
    Public void Save(Customer _customer)
    _customer.SetAllModified(contextDB);    //Update EntityState for contextDB.
    contextDB.SaveChanges();                //Only Customer record is saved. Addresses don't get saved.
    --Public helper method.
    public static void SetAllModified<T>(this T entity, ObjectContext context) where T : IEntityWithKey
    var stateEntry = context.ObjectStateManager.GetObjectStateEntry(entity.EntityKey);
    var propertyNameList = stateEntry.CurrentValues.DataRecordInfo.FieldMetadata.Select
      (pn => pn.FieldType.Name);
    foreach (var propName in propertyNameList)
    Monday, September 12, 2011 2:16 PM