How do I add an Entity from a Winforms details form? RRS feed

  • Question

  • Hi,

    Studying Julie Lerman's EF4 book and have encountered a simple scenario for which I cannot find a solution.

    I've dragged details of an Entity to a Windows Form. Along with a BindingSource, a BindingNavigator is added.

    I have no problem programmatically instantiating an Entity, providing values and saving it to the database. However, I can't figure out how to do this using the controls on the form itself.

    I'm handling BindingNavigatorAddNewItem.Click, wherein I instantiate a new Entity, provide a couple of auditing values such as the user who created it and the date created. I then add it to the ObjectSet:

      Dim newUnit As New Unit
      With newUnit
       .CreateUser = CurrentUser
       .CreateDate = Now
       .ModifyUser = CurrentUser
      End With
    From here, I can't figure out what to do. If I provide all values in the form, then call context.SaveChanges, a System.Data.Update exception is thrown, reporting that I didn't provide a value for a required field. Can someone clue me in as to what to do, please? Julie explains how to do this in code but does not discuss how to add an entity from a form.
    Monday, August 15, 2011 12:08 AM


  • The problem was that both Ms. Lerman and the tutorial linked above would have us binding our DataGridView to the results of a query which are output to a List.

    While this method will result in a populated DataGridView, you will not be able to update or insert. Instead, bind your DataGridView directly to the to the results of the query (ie the ObjectQuery variable).

    • Marked as answer by vba-dev Monday, August 22, 2011 11:00 PM
    Monday, August 22, 2011 11:00 PM

All replies