Following on from these posts;
ADO.NET Data Services - Exposing Arbitrary Data (1)
ADO.NET Data Services - Exposing Arbitrary Data (2)
what if I want to delete my newly inserted data? Against the Entity Framework data source I can write something like;
demoEntities efService = new demoEntities("http://localhost:32767/WebSite6/EFService.svc");
efService.MergeOption = Microsoft.Data.WebClient.MergeOption.AppendOnly;
author lee = (from a in efService.author
where a.lastName == "Lee"
select a).Single();
efService.DeleteObject(lee);
efService.LoadProperty(lee, "book");
foreach (book b in lee.book)
{
efService.DeleteObject(b);
}
efService.SaveChanges();
and that seems to work fine. I've a sneaking suspicion that I'm really meant to call DeleteBinding as well to delete the relationship between the book and its author but deleting the two entities seems to do the trick - I'm not sure where it wouldn't do the trick but I've a suspicion it might be where I have a many-to-many relationship which also needed deleting (that's just a suspicion).
To do this against my in-memory service I need to implement the Delete method of IUpdatable on my existing BookContext class - I went this way but I'm really not too sure about it;
public void Delete(object resource)
{
// My data structures are a bit poor for implementing
// this delete method.
KeyedEntity entity = (KeyedEntity)resource;
CollectionInfo ci = collections[entity.GetType()];
try
{
// Note - we may acquire this more than once here.
ci.Lock.AcquireWriterLock(-1);
((IList)ci.List).Remove(entity);
DeleteBook(entity as Book);
DeleteAuthor(entity as Author);
}
finally
{
ci.Lock.ReleaseWriterLock();
}
}
private void DeleteBook(Book b)
{
if ((b != null) && (b.Author != null))
{
b.Author.Books.Remove(b);
}
}
private void DeleteAuthor(Author a)
{
if ((a != null) && (a.Books != null))
{
for (int i = a.Books.Count - 1; i >= 0; i--)
{
Delete(a.Books[i]);
}
}
}
One reason why I'm not too sure on it is because I'm not sure how a call to Delete and/or DeleteBinding translates from calls on the client side to calls on the service-side. I can see if that if I call Delete client-side then that shows up service-side whereas if I also call DeleteBinding then I see no difference other than my navigation properties are set to NULL service-side when my call to Delete() occurs.
Now, for me, this is a bit of a problem (poor data structures on my part) because if you delete a Book then I need to make sure that it's also deleted from the Books collection of its Author and so I need to know which Author wrote the book and navigating from the Book to the Author is kind of handy. Naturally, I could build some dictionaries that took care of this but it's still kind of handy.
Hence, in the client-code below I've commented out the DeleteBinding() calls because I'd need to rework my data structures to make that work service-side (whether that's the right thing to do or not, I'm not 100% sure at this point);
BookContext memService = new BookContext("http://localhost:32767/WebSite6/MemoryService.svc");
memService.MergeOption = Microsoft.Data.WebClient.MergeOption.AppendOnly;
Author lee = (from a in memService.Authors
where a.LastName == "Lee"
select a).Single();
memService.LoadProperty(lee, "Books");
foreach (Book b in lee.Books)
{
//memService.DeleteBinding(lee, "Books", b);
//memService.DeleteBinding(b, "Author", lee);
memService.DeleteObject(b);
}
memService.DeleteObject(lee);
memService.SaveChanges();
So, this isn't anywhere near perfect and the post is getting long so I'll add another post about updating.
Posted
Thu, Jan 3 2008 3:53 AM
by
mtaulty