Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7967581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:56:50+00:00 2026-06-04T06:56:50+00:00

how i can use Self-Tracking Entities in WCF Services to implement ServiceContract this my

  • 0

how i can use Self-Tracking Entities in WCF Services to implement ServiceContract

this my ServiceContract in IPbService.cs

namespace PhoneBookService
{
     [ServiceContract]
     public interface IPbService
     {
          [OperationContract]
          List<User> GetAllUser();

          [OperationContract]
          string AddUser(User user);

          [OperationContract]
          string DeleteUser(int id);

          [OperationContract]
          string UpdateUser(User user);

          [OperationContract]
          List<Contact> GetContactsByUser(int id);

          [OperationContract]
          string AddContact(Contact contact, string userName);

          [OperationContract]
          string DeleteContact(int id);

          [OperationContract]
          string UpdateContact(Contact contact);


     }
}

and this is my implemention class in PbService.svc.cs

namespace PhoneBookService
{
     public class PbService : IPbService
     {
          readonly PhoneBookDBEntities _context = new PhoneBookDBEntities();

          public List<User> GetAllUser()
          {
                return _context.Users.OrderBy(u => u.Name).ToList();
          }

          public string AddUser(User user)
          {
            try
            {
                _context.Users.AddObject(user);
                 _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }
          }

          public string DeleteUser(int id)
          {
            try
            {
                User user = _context.Users.FirstOrDefault(u => u.UserID == id);
                _context.Users.DeleteObject(user);
                _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }

          }

          public string UpdateUser(User user)
          {
            try
            {
                if (user == null) throw new ArgumentNullException("user");
                User oldUser = _context.Users.FirstOrDefault(u => u.UserID == user.UserID);
                if (oldUser != null)
                {
                    oldUser.Name = user.Name;
                    oldUser.Password = user.Password;
                    oldUser.UserName = user.UserName;
                    oldUser.Email = user.Email;
                    oldUser.CreationDate = DateTime.Now;
                }
                _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }

          }

          public List<Contact> GetContactsByUser(int id)
          {
                User user = _context.Users.FirstOrDefault(u => u.UserID == id);
                if (user == null) throw new ArgumentNullException("id");
                return user.Contacts.OrderBy(c=>c.Name).ToList();
          }

          public string AddContact(Contact contact, string userName)
          {
            try
            {
                User user = _context.Users.FirstOrDefault(u => u.UserName == userName);
                if (user != null) user.Contacts.Add(contact);
                _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }

          }

          public string DeleteContact(int id)
          {
            try
            {
                Contact contact = _context.Contacts.FirstOrDefault(c => c.ContactID == id);
                _context.Contacts.DeleteObject(contact);
                _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }
          }

          public string UpdateContact(Contact contact)
          {
            try
            {
                Contact oldContact = _context.Contacts.FirstOrDefault(c => c.ContactID == contact.ContactID);
                if (oldContact != null)
                {
                    oldContact.Name = contact.Name;
                    oldContact.PhoneNumber = contact.PhoneNumber;
                    oldContact.Email = contact.Email;
                    oldContact.Mobil = contact.Mobil;
                    oldContact.Address = contact.Address;
                }
                _context.SaveChanges();
                return "";
            }
            catch (Exception e)
            {
                return e.Message;
            }
          }

     }
}

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:56:51+00:00Added an answer on June 4, 2026 at 6:56 am

    In Your WCF project you just need to reference your EntityClasses project. That simple?! I guess so…

    Your Self-Tracking Entities are already equipped with the right DataMember attributes for the properties it carries.

    Another thing…I see you use distinct methods for Add, Update and Delete. I always use a single Persist method that something goes like this:

    using(Entities context = new Entities())
    {
      try
      {
        context.ApplyChanges(user);
        context.SaveChanges();
      }
      catch
      {
        ...
      }
    }
    

    The Self-Tracking Entities context “knows” how to apply changes made to entities based on the ChangeTracker the STE is carrying. So you don’t need seperate methods at all. Very easy to maintain.

    So when you create a new entity in some client-application the ChangeTracker.State will be ObjectChangeTrackerState.Add and when modifying an existing entity it will be Modified and Deleted when you use entity.MarkAsDeleted().

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP classes can use the keyword self in a static context, like this: <?php
I've been reading about self-tracking entities in .net and how they can be generated
I have a WCF client which passes Self-Tracking Entities to a WPF application built
How can I access deleted entity in self tracking entities graph? I understand that
It seems I can use self or this for referring to the mixed-in instance
I want to serialize an Entity Framework Self-Tracking Entities full object graph (parent +
i can use MyFirstAppDelegate *delegate = (MyFirstAppDelegate *)[[UIApplication sharedApplication] delegate]; [delegate.navigationController popViewControllerAnimated:YES]; or [self.navigationController
Can I use self = nil in an instance method so that when the
I read on MSDN that to improve scripting efficiency, you can use self to
I have a class that doesn't extend webapp.RequestHandler , and I can't use self.response.out.write()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.