private TDataObj _GetDataObject(EntityObject entity) { TDataObj obj = null; if (_owner.IsStored) { // Try to find data object through IWrapDataAccess. obj = DataObjectHelper.GetDataObject(entity) as TDataObj; if (obj == null) { DataObjectContext ctx = ContextHelper.GetObjectContext( _entityRef); obj = DataObjectHelper.GetOrCreateDataObject <TDataObj>(ctx, entity); } } else { obj = DataObjectHelper.GetDataObject(entity) as TDataObj; if (obj == null) { throw new DataException(Properties.Messages.Error_InvalidDataObjectInstance); } } return(obj); }
private void _CheckCanSave(ObjectStateEntry entry) { Debug.Assert(entry != null); if (entry.State == EntityState.Added) { DataObject obj = DataObjectHelper.GetDataObject(entry.Entity as EntityObject); if (obj != null) { if (!obj.CanSave) { this.Detach(entry.Entity); } } } }
/////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// private void ObjectStateManager_Changed(object sender, CollectionChangeEventArgs e) { if (e.Action == CollectionChangeAction.Refresh) { _NotifyContextCollectionChanged(e); } else { if (e.Element != null && e.Element is EntityObject) { EntityObject entity = e.Element as EntityObject; if (entity.EntityState != EntityState.Detached) { object obj = DataObjectHelper.GetDataObject(entity); if (obj != null && obj.GetType() == typeof(T)) // NOTE: use exact types comparison, derived types are not accepted { _NotifyContextCollectionChanged(e, obj as T); } } } } }
private void DataObjectContext_SavingChanges(object sender, EventArgs e) { IEnumerable <ObjectStateEntry> entries = this.ObjectStateManager.GetObjectStateEntries( EntityState.Added | EntityState.Modified | EntityState.Deleted); List <DataObject> addedItems = new List <DataObject>(); List <DataObject> modifiedItems = new List <DataObject>(); List <DataObject> deletedItems = new List <DataObject>(); foreach (ObjectStateEntry entry in entries) { if (!entry.IsRelationship && entry.EntitySet.ElementType is EntityType) { if (entry.State == EntityState.Added || entry.State == EntityState.Modified) { // apply database constraints _ApplyConstraints(entry); // check whether object can be saved _CheckCanSave(entry); // fill event collections if (entry.State != EntityState.Detached) { DataObject item = DataObjectHelper.GetDataObject( entry.Entity as EntityObject); if (item != null) { if (entry.State == EntityState.Added) { addedItems.Add(item); } else if (entry.State == EntityState.Modified) { if (_IsMarkedAsDeleted(item)) { deletedItems.Add(item); } else { modifiedItems.Add(item); } } } } } else if (entry.State == EntityState.Deleted) { DataObject item = DataObjectHelper.GetDataObject( entry.Entity as EntityObject); if (item != null) { deletedItems.Add(item); } } } } _NotifySavingChanges(addedItems.AsReadOnly(), modifiedItems.AsReadOnly(), deletedItems.AsReadOnly()); }