Best way to attach row from datagrid to EF.

Posted by AKoran on Stack Overflow See other posts from Stack Overflow or by AKoran
Published on 2010-06-17T13:44:24Z Indexed on 2010/06/17 15:53 UTC
Read the original article Hit count: 165

Filed under:
|
|

Using MVVM and EF...I have a datagrid binding to a View Model (using ObservableCollection). The view model has a save command which simply calls the SaveChanges command of the Data Context. However, when a user adds a new row to the datagrid, the new entity is detached. Is there any easy way to automatically attach it when it gets created. Currently, I'm having to do this in the Save command of my View Model and it seems a bit clunky:

        foreach (var dataItem in _DataList)  // where _DataList is the ObservableCollection
        {
            if (dataItem.EntityState == EntityState.Detached)
            {
                _DataContext.AddToTestTables(dataItem);
            }
        }
        _DataContext.SaveChanges();

© Stack Overflow or respective owner

Related posts about wpf

Related posts about entity-framework