Self-Tracking Entities with One to many relationship

131 Views Asked by At

I Use Self-Tracking Entities with WCF to Work With data and asp.net MVC3 As a client any idea why this code doesn't work

in my MVC Controller i have this action to get User Contacts

      public ActionResult Contacts(int id)
      {
            var contacts = _proxy.GetContactsByUser(id);
            var mcontacts = Mapper.Map<Contact[], MContact[]>(contacts);

            return View(mcontacts);
      }

in WCF Service this is my method to get User contacts

      public List<Contact> GetContactsByUser(int id)
      {

                 var user = _context.Users.FirstOrDefault(u => u.UserID == id);
                List<Contact> contacts = user.Contacts.ToList();
                return contacts;

      }

my problem is List<Contact> contacts is Always null

1

There are 1 best solutions below

4
On

what exactly doesn't work??

form the first look of it you have not created a map

 public ActionResult Contacts(int id)
      {
            var contacts = _proxy.GetContactsByUser(id);
            Mapper.CreateMap<Contact,MContact>();
            var mcontacts = Mapper.Map<Contact[], MContact[]>(contacts);

            return View(mcontacts);
      }

here is a useful link

http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home