locked
Adding child entities to a new parent entity in Lightswitch 2012 (non-HTML) RRS feed

  • Question

  • I have a parent entity - for the sake of understanding, we'll call it "contact".  Now, let's imagine that the contact entity has a collection of phone number entities.  I would like to be able to add multiple phone number entities (home, fax, mobile) to a new contact in the same screen where I am creating the contact.  It is easy to do after the contact has been saved, as I just pass the existing contact entity id as a parameter to the "AddNewPhoneToContact" screen.  But what do I do when the contact entity has not yet been saved?

    Possible?

    Monday, May 13, 2013 6:23 PM

All replies

  • You can have a create new contact screen which has also a grid with phone numbers.

    There is no problem adding new phone numbers while there is not yet the contact saved in the db.


    paul van bladel

    Tuesday, May 14, 2013 5:30 AM
  • If you've created a relationship between the two tables (in the table designer), the parent entity (Contact) will have a navigation collection property for the related child entities (PhoneNumbers).

    If you drag the navigation property, to the screen it will be represented by a grid, where you can add/edit/delete child records.

    Or you can specify to display the child entities in the Add New Screen wizard to create the detail screen (there's a checkbox for each related child entity) to have the wizard do it for you.

    If you were doing it in code, you'd simply add the new PhoneNumber record to the parent Contact's PhoneNumbers collection & the setting of the parent ID will be done for you, behind the scenes.

    You can use this method, even if the parent entity hasn't been saved yet. When the screen's DataWorkspace submits its changes, LightSwitch (actually Entity Framework, which LightSwitch uses) knows to save the parent entity before saving any child entities.

    The navigation collection properties really make what you're wanting to do very easy, no matter which of the three scenarios you're using.


    Yann Duran
         - Co-Author of Pro Visual Studio LightSwitch 2011
         - Author of the  LightSwitch Central Blog

    FREE Download: Luminous Tools for LightSwitch
    (a Visual Studio productivity extension for LightSwitch)
     
    Click Mark as Answer, if someone's reply answers your question
    Click  Vote as Helpful, if someone's reply is helpful
     
    By doing this you'll help everyone find answers faster.

    Tuesday, May 14, 2013 1:50 PM
    Moderator
  • Thanks for your response.  I should have added that adding the phone # requires some additional parameters that necessitate an "AddNew" screen.  Let's say you have to set a department code value that requires two levels of dependent filtered drop-downs.  This AddNew screen works when adding a phone to an existing contact, but not to the new contact.

    I had already set up the "Add New" Contact screen as you mention and could add items to the PhoneNumber grid and save just fine.  But I am now overriding the "Add..." command bar item button with an "Add New" phone number screen that supports some required logic to set the "department" value, but I get the error "Unable to save data - Please correct data entry errors and try to save again" and highlights the "Contact" entity, which is empty (because it is new contact) in the form.

    So when you suggest "doing it in code", do you mean I implement the "PhoneNumber_Save" method and somehow add the new PhoneNumber entity to the new parent Contact's PhoneNumber collection?  I'm sorry, but I don't know how to get access to the parent Contact object in the context of the PhoneNumber screen.

    Sorry for the naivite.  I'm an experienced developer that has been working with Lightswitch for about a week.

    Your help is much appreciated.



    • Edited by KalaniK Tuesday, May 14, 2013 4:14 PM
    Tuesday, May 14, 2013 4:10 PM
  • Oh sorry! I wasn't suggesting that the grid be an editable grid. You can still override the grid's buttons & call your Add New screen. But by doing it this way, again, you don't have to worry about setting the parent ID, it will be done for you.

    Yann Duran
         - Co-Author of Pro Visual Studio LightSwitch 2011
         - Author of the  LightSwitch Central Blog

    FREE Download: Luminous Tools for LightSwitch
    (a Visual Studio productivity extension for LightSwitch)
     
    Click Mark as Answer, if someone's reply answers your question
    Click  Vote as Helpful, if someone's reply is helpful
     
    By doing this you'll help everyone find answers faster.

    Wednesday, May 15, 2013 6:02 AM
    Moderator
  • "Sorry for the naivite.  I'm an experienced developer that has been working with Lightswitch for about a week"

    No need to apologise! We all started out where you're starting out now. LightSwitch does require experienced developers to think a bit differently, but once you get it, it'll all make perfect sense.

    LightSwitch does things a particular way. If you work with it, & not against it, it's mostly pretty easy. It just takes a bit of time to get used to, & usually lots of questions. :-)


    Yann Duran
         - Co-Author of Pro Visual Studio LightSwitch 2011
         - Author of the  LightSwitch Central Blog

    FREE Download: Luminous Tools for LightSwitch
    (a Visual Studio productivity extension for LightSwitch)
     
    Click Mark as Answer, if someone's reply answers your question
    Click  Vote as Helpful, if someone's reply is helpful
     
    By doing this you'll help everyone find answers faster.

    Wednesday, May 15, 2013 6:05 AM
    Moderator
  • Thanks Yann.  Had to step away from this for a bit, but now I'm back.

    When I override the Add New button with a screen I've created for the child entity using the wizard, validation fails for the parent entity Id.  Ideas? 

    Will letting you know that I bought the Kindle version of your book, which I highly recommend and was helpful with some other tricky bits, keep you from feeling pestered by me?

    Thursday, May 23, 2013 6:22 PM