Language: C#
EF4 Insert, Update, Delete in a WCF method
[OperationContract] public Contact SaveContact(Contact entity) { bool _IsDeleted = false; string _EntitySetName; _EntitySetName = "Contacts"; using (CometEntities _Context = new CometEntities()) { switch (entity.ChangeTracker.State) { case ObjectState.Deleted: //delete _IsDeleted = true; _Context.AttachTo(_EntitySetName, entity); _Context.DeleteObject(entity); break; default: //everything else _Context.Contacts.ApplyChanges(entity); break; } try { // Try to save changes, which may cause a conflict. _Context.SaveChanges(System.Data.Objects.SaveOptions.None); } catch (System.Data.OptimisticConcurrencyException) { // Resolve the concurrency conflict by refreshing the // object context before re-saving changes. _Context.Refresh(System.Data.Objects.RefreshMode.ClientWins, entity); // Save changes. _Context.SaveChanges(); } } if (_IsDeleted) return null; entity.AcceptChanges(); return entity; }
Tags:
Description:
Save a Contact (http://jerrytech.blogspot.com/2010/06/ef4-insert-update-delete-in-wcf-method.html)
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search


Updated 5/5/2011