none
Why the ObjectContext does not recognize the new record when I call AddNew on the BindingNavigator? RRS feed

  • Question

  • Hi,
    I'm new of using the entity framework to do update, insert, and delete to the sql server database.
    I have two tables, and a customer can have multiple orders, so it's one-many:
    1.Customer
    CustID,
    FirstName,
    LastName
    2.Order--
    OrderID,
    CustID,
    OrderDescription
    The update works fine when I edit the data on the textboxes, but when I call add  new on the binding navigator, the context doesn't seem to recognize the new customer record, so when context.savechange() is called, I don't see the new record is inserted to the database.  Here is my code:
    private void Form1_Load(object sender, EventArgs e)
     {
                _context = new AutoLotModel.ALEntities();
                customerBindingSource.DataSource = _context.Customers.ToList();
      }
      private void customerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
      {
                customerBindingSource.EndEdit();            
                _context.SaveChanges();
      }
    Friday, December 30, 2011 7:01 PM

Answers

  • Hi,

    make sure you bind the QueryObject or ObjectSet results directly to the BindingSource, rather than converting those results to a List(Of T) and binding it.

    // Execute the query and bind the result to the OrderItems control.
    this.orderItemsGrid.DataContext = ((ObjectQuery)query).Execute(MergeOption.AppendOnly);


    see the following section on msdn (it contains how-tos as well):

    http://msdn.microsoft.com/en-us/library/bb738469.aspx

     

    Also, here is a data binding example using EF 4.0

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=619

     

    If you are want to use EF 4.1 or 4.2, see

    http://msdn.microsoft.com/en-us/library/gg197522%28VS.103%29.aspx

    http://msdn.microsoft.com/en-us/library/gg197523%28VS.103%29.aspx

     

    thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, December 30, 2011 11:16 PM
    Moderator

All replies

  • Hi,
    I'm new of using the entity framework to do update, insert, and delete to the sql server database.
    I have two tables, and a customer can have multiple orders, so it's one-many:
    1.Customer
    CustID,
    FirstName,
    LastName
    2.Order--
    OrderID,
    CustID,
    OrderDescription
    The update works fine when I edit the data on the textboxes, but when I call add  new on the binding navigator, the context doesn't seem to recognize the new customer record, so when context.savechange() is called, I don't see the new record is inserted to the database.  Here is my code:
    private void Form1_Load(object sender, EventArgs e)
     {
                _context = new AutoLotModel.ALEntities();
                customerBindingSource.DataSource = _context.Customers.ToList();
      }
      private void customerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
      {
                customerBindingSource.EndEdit();            
                _context.SaveChanges();
      }
    Friday, December 30, 2011 7:05 PM
  • Hi,

    make sure you bind the QueryObject or ObjectSet results directly to the BindingSource, rather than converting those results to a List(Of T) and binding it.

    // Execute the query and bind the result to the OrderItems control.
    this.orderItemsGrid.DataContext = ((ObjectQuery)query).Execute(MergeOption.AppendOnly);


    see the following section on msdn (it contains how-tos as well):

    http://msdn.microsoft.com/en-us/library/bb738469.aspx

     

    Also, here is a data binding example using EF 4.0

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=619

     

    If you are want to use EF 4.1 or 4.2, see

    http://msdn.microsoft.com/en-us/library/gg197522%28VS.103%29.aspx

    http://msdn.microsoft.com/en-us/library/gg197523%28VS.103%29.aspx

     

    thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, December 30, 2011 11:16 PM
    Moderator
  • Thanks a lot, It works.  And the article also explains the reasons.
    Saturday, December 31, 2011 12:22 AM