开发者

Using Reflection to Remove Entity from RIA Services EntityCollection?

To facilitate control reuse we created a solution with three separate projects: a control library, Silverlight client, and ASP.NET backend. The control library has no reference to the RIA Services-generated data model classes so when it needs to interact with it, we use reflection.

This has worked fine so far but I've hit a bump. I have a DataGrid control where the user can select a row, press the 'delete' button, and it should remove the entity from the collection. In the DataGrid class I have the following method:

private void RemoveEntity(Entity entity)
{
    // Use reflection to remove the item from the collection
    Type sourceType = typeof(System.Windows.Ria.EntityCollection<>);
    Type genericType = sourceType.MakeGenericType(entity.GetType());
    System.Reflection.MethodInfo removeMethod = genericType.GetMethod("Remove");
    removeMethod.Invoke(this._dataGrid.ItemsSource, new object[] { entity });

    // Equivalent to: ('Foo' derives from Entity)
    //   EntityCollection<Foo> ec;
    //   ec.Remove(entity);
}

This works on the client side but on the domain service the following error gets generated during the Submit() method:

"The UPDATE statement conflicted with the FOREIGN KEY constraint "********". The conflict occurred in database "********", table "********", column '********'. The statement has been terminated."

One thing I noticed is the UpdateFoo() service method is being called instead of the DeleteFoo() method on the domain service. Further inspection shows the entity is going into the ModifiedEntities ChangeSet instead of the RemovedEntities ChangeSet. I don't know if that's the problem but it doesn't seem right.

Any help would be appreciated, thanks,


UPDATE

I've determined that the problem is definitely coming from the reflection call to the EntityCollection.Remove() method. For some reason calling it causes the entity's EntityState property to change to EntityState.Modified instead of EntityState.Deleted as it should.

Even if I try to remove from the collection by completely circumventing the DataGrid I get the exact same issue:

Entity selectedEntity = this.DataContext.GetType().GetProperty("SelectedEntity").GetValue(this.DataContext, null) as Entity;
object foo = selectedEntity.GetType().GetProperty("Foo")开发者_StackOverflow.GetValue(selectedEntity, null);
foo.GetType().InvokeMember("Remove", BindingFlags.InvokeMethod, null, foo, new object[] { entity });

As a test, I tried modifying the UpdateFoo() domain service method to implement a delete and it worked successfully to delete the entity. This indicates that the RIA service call is working correctly, it's just calling the wrong method (Update instead of Delete.)

public void UpdateFoo(Foo currentFoo)
{
    // Original update implementation
    //if ((currentFoo.EntityState == EntityState.Detached))
    //    this.ObjectContext.AttachAsModified(currentFoo, this.ChangeSet.GetOriginal(currentFoo));

    // Delete implementation substituted in
    Foo foo = this.ChangeSet.GetOriginal(currentFoo);
    if ((foo.EntityState == EntityState.Detached))
        this.ObjectContext.Attach(foo);
    this.ObjectContext.DeleteObject(foo);
}


I've been researching a similar issue.

I believe the issue is you are calling remove with a reference for an EntityCollections within the DomainContext as the root reference rather than using the DomainContext itself as the root.

So...

ParentEntityCollection.EntityCollectionForTEntity.Remove(TEntity);

Produces the EntityState.Modified instead of EntityState.Deleted

Try instead...

DomainContext.EntityCollectionForTEntity.Remove(TEntity);

I think this will produce the result you are seeking.

Hope this helps.


What is the "column" in the "FOREIGN KEY constraint" error? Is this a field in the grid row and collection that coorosponds to that column? Is it possible that the entity you are trying to remove is a column in the row rather than the row itself which is causing an update to the row (to null the column) rather than to delete the row?


I read your update and looks like you've determined that the problem is the reflection.

Have you tried to take the reflection out of the picture?

As in:

private void RemoveEntity(Entity entity) 
{ 
    // Use reflection to remove the item from the collection 
    Type sourceType = typeof(System.Windows.Ria.EntityCollection<>); 
    Type genericType = sourceType.MakeGenericType(entity.GetType()); 

    // Make sure we have the right type
    // and let the framework take care of the proper invoke routine
    if (genericType.IsAssignableFrom(this._dataGrid.ItemsSource.GetType()))
        ((Object) this._dataGrid.ItemsSource).Remove(entity);
} 

Yes, I know it's ugly, but some times...

Edited to add

I've updated the code to remove the is keyword.

Now about using the object to make the call to the Remove method, I believe it might work due the late binding of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜