I’m developing simple Silverlight 4 application based on one-to-many association.
The model of relation is presented here.
The page consists of two grids, one is loaded straightly from DomainDataSource (ExcelTemplate one). After selecting ExcelTemplate the second one is loaded with ColumnValidationRules from ObservableCollection which is filled on base of ExcelTemplate‘s list. Entities does not have OnDelete actions. I have a lot of problems with deleting ColumnValidationRule objects family. Basically I’ve tried 2 different approaches:
- Deleting only ColumnValidationRule using
ColumnValidationRuleDomainDataSource (based on GetColumnValidationRule query):
XAML with my DomainDataSources:
<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:ExcelTemplate, CreateList=true}" Height="0" LoadedData="ExcelTemplateDomainDataSource_LoadedData" Name="excelTemplatesForValidatiorsDDS" QueryName="GetExcelTemplatesQuery" Width="0">
<riaControls:DomainDataSource.DomainContext>
<my1:SRFDomainContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:ColumnValidationRule, CreateList=true}" Height="0" LoadedData="ExcelTemplateDomainDataSource_LoadedData" Name="columnValidationRulesDomainDataSource" QueryName="GetColumnValidationRulesQuery" Width="0">
<riaControls:DomainDataSource.DomainContext>
<my1:SRFDomainContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
Code:
// Filling DataViews:
this.excelTemplatesForValidatiorsDDS.Load();
this.columnValidationRulesDomainDataSource.Load();
// Delete action:
private void DeleteRuleButton_Click(object sender, RoutedEventArgs e)
{
this.columnValidationRulesDomainDataSource.Load();
ColumnValidationRule cvr = this.srfValidatorsDataGrid.SelectedItem as ColumnValidationRule;
this.columnValidationRulesDomainDataSource.DataView.Remove(cvr);
}
In result im getting an error:
The specified entity is not contained in this EntitySet.
Error details:
w System.ServiceModel.DomainServices.Client.EntitySet.Remove(Entity entity)
w System.Windows.Controls.PagedEntityCollectionView.RemoveCore(Entity item, Int32 index)
w System.Windows.Controls.PagedEntityCollectionView.RemoveItem(Entity item)
w System.Windows.Controls.EntityCollectionView.Remove(Object item)
w System.Windows.Controls.DomainDataSourceView.Remove(Object item)
w LANOS.UserManagement.DeleteRuleButton_Click(Object sender, RoutedEventArgs e)
w System.Windows.Controls.Primitives.ButtonBase.OnClick()
w System.Windows.Controls.Button.OnClick()
w System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
w System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
w MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventN
- Creating different DomainDataSource for each of objects family:
ColumnValidationRule, AssociatedColumn, ColumnValue. Then deleting them using those DomainDataSource’s and submitting from the
tail to head. This approach also failed. I had similar error to
those in 1st approach. Also from time to time some of those
DomainDataSource’s were empty.
I was googling after examples which would cover this problem but I have not found any. Appreciating any tips, links or help 😉
UPDATE1:
I have used your advice:
this.columnValidationRulesDomainDataSource.Load();
ColumnValidationRule deletedObject = null;
foreach (ColumnValidationRule cvr in this.columnValidationRulesDomainDataSource.DataView)
{
if (cvr.ID.Equals((this.srfValidatorsDataGrid.SelectedItem as ColumnValidationRule).ID))
{
deletedObject = cvr;
}
}
this.columnValidationRulesDomainDataSource.DataView.Remove(deletedObject);
this.columnValidationRulesDomainDataSource.SubmitChanges();
Now im getting this error: The item provided is not instance of type Entity :/
UPDATE 2:
I’ve added on SubmitedChanges Event handler and as I suspected in the beginning of the post, there is a problem with deleting caused by Foreign Keys:
e.Error.Message "Submit operation failed. An error occurred while updating the entries. See the inner exception for details. InnerException message: The DELETE statement conflicted with the REFERENCE constraint \"FK_AssociatedColumn_ColumnValidationRule\". The conflict occurred in database \"LANOS\", table \"dbo.AssociatedColumns\", column 'ColumnValidationRuleID'.\r\nThe statement has been terminated."
Unfortunately I’m not able to set up model to make use of End On Delete – Cascade property.
Setting Cascade deleting cause compilation error:
End 'ColumnValue' on relationship 'LANOSModel.FK_AssociatedColumnColumnValue' cannot have operation specified since its multiplicity is '*'. Operations cannot be specified on ends with multiplicity '*'.
The reason “the specified entity is not contained in this EntitySet” is because…. wait for it… the specified instance of that entity really is not contained in the EntitySet.
You have apparently loaded the data grid from a previous data context, whereas
this.columnValidationRulesDomainDataSource.Load()is creating a new set of entities, so you cannot remove your specific old object from a new data set. They have the same values but are not the same objects.You can however use a key from the object and do a delete on the new context (where the id’s match). Just find the matching instance first and delete that.
If you can provide more details/code I might be able to provide a specific example, but you should have enough to go on now.