WCF RIA Services v1 sp1, Composition, validate child entities
I'm using composition attribute. Recently I came into the following blog:
http://brettsam.com/2011/03/25/ria-services-composition-with-entity-framework/
So, I used the approach described in the above blog post to correct entity state of my child entities like the following:
foreach (Child c in this.ChangeSet
.GetAssociatedChanges(curren开发者_JS百科tParent, p => p.Children))
{
ChangeOperation change = this.ChangeSet.GetChangeOperation(c);
switch (change)
{
case ChangeOperation.Delete:
...
case ChangeOperation.Insert:
// This is already done for us.
break;
case ChangeOperation.None:
...
case ChangeOperation.Update:
this.ObjectContext.Children
.AttachAsModified(c, this.ChangeSet.GetOriginal(c));
break;
default:
break;
}
}
I also removed UpdateChild(Child currentChild) method which was generated by default. Now, the code works and child entities are saved to database as expected. However, I noticed one problem. My child entity's some properties had custom validation attribute (inherited from ValidationAttribute class). They were not called at all. This custom validation attribute is not generated at the silverlight client side, because it uses some classes which are only available in .NET. but not in silverlight. So, at the client validation passes, and server-side doesn't validate either. However, if I add UpdateChild method back to the DomainService, the validation attribute's code runs. What's wrong here? Can someone explain this?
You should take a look at the EntityGraph
It's more powerful than composition and from my experience it works well - including the kind of validation scenario you have described. In fact you can do way more complicated cross entity validations with the graph.
精彩评论