0

I want to bind a Datagrid to navigation property.

for example.If I have two entities Clients and phones linked by Client_ID

No problem when user edit phone details,modification is saved correctly

the problem exist when users add new Phone and save,the new row is not saved. also If user add new client. client data is saved but all phones are not saved? Thanks for help

2 Answers 2

0
 private void Save_Button_Click(object sender, RoutedEventArgs e)
    {
        Client1.PhoneNumbers.ToList().ForEach(pn => pn.Client = Client1);
        Client1.OtherNavigationProperties = null;
        using(var context=new MyContext())
        {
            if (insert)
                context.Client.Add(Client1);
            if (update)
            {
                var client = context.GetClientsByClientID(Client1.ID);
                client.PhoneNumbers = Client1.PhoneNumbers;
                //other changes if any in client
                //again set all navigation properties null else than PhoneNumbers

            }
            context.SaveChanges();
        }
    }

Try this one

0
0

this might be because Phone will have the Property Client When you add Phone then this property is null, so You will also have to set its Client Property to the Current client or more exactly the client whose id is going to be in Phone F.K . Hope this will help.

public void SaveClient(Client client)
        {
            client.PhoneNumbers.ToList().ForEach(pn=>pn.Client=client);
            using(var context=new MyContext())
            {
                  context.Clients.Add(client);
                  context.SaveChanges();
             }

        }
8
  • Thanks For Reply. I deleted the client property from Phone. But it didn't work
    – Sam1970
    Jul 5, 2012 at 10:32
  • No you can't delete it then it will not work ,All you need to do is to set the client to it when you send it for save or when you initialize Phone object.
    – yo chauhan
    Jul 5, 2012 at 10:44
  • Users add new phone from WPF Datagrid. Could you tell me which datagrid event should I use to set the client to phone. I'll appreciate if you could provide code sample.
    – Sam1970
    Jul 5, 2012 at 10:54
  • just do that when you send the Client to save from ViewModel or you can do that in DAL/Repository . Just before saving the client.
    – yo chauhan
    Jul 5, 2012 at 10:58
  • User might add as many phone as they like also ther are a lot of detailed table linked to the client and have the same problem.I don't know a lot about Dal/Repository Pattern.
    – Sam1970
    Jul 5, 2012 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.