views:

80

answers:

1

I tried following the steps I describe below in VS 2010, but to no avail:

  • Create new Windows Forms App. Project
  • Add new empty ADO EDM to Project
  • Add new entity Customer with scalar properties FirstName and LastName
  • Generate db script
  • Execute db script to create tables in db
  • Add new Object Data Source based on EDM Model1
  • Select object Customer in Data Sources window and drop it on form in form of Details view.
  • All typical binding controls are created, all I need to do is to enable the Save button.

I run the app, form is displayed. I can add new records and navigate between them. I press save, but when I reinitialize the app, all data is lost.

Is it possible to make this work with 0 code, if not, what is the minimum I should add to make this work.

+2  A: 
Thomas Levesque
What db file do you refer to exactly? My EDM points to database in Sql Server and I would expect changes to be persisted to DB ie. new records should be visible as data in a table in a database.
Dan
OK, you're not using an attached .mdf file ?
Thomas Levesque
see my updated answer
Thomas Levesque
Only after adding following code am I able to persist to DB. However, binding is still not working since data is not loaded initially. *** Public Class Form1 Dim ctx As Model1Container = New Model1Container() Private Sub CustomerBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerBindingNavigatorSaveItem.Click Dim c = Customer.CreateCustomer(IdTextBox.Text, FirstNameTextBox.Text, LastNameTextBox.Text) ctx.Customers.AddObject(c) ctx.SaveChanges() End Sub End Class ***
Dan
You need to set the BindingSource's DataSource property to an instance of the entity set : `customerBindingSource.DataSource = ctx.Customers;`
Thomas Levesque
Even after setting the DataSource, the code is still flawed. It will work only if I press Save after adding each new item. If I add 3 in a row and press Save it will not work. **Could you by any chance post the whole code for this trivial example?**
Dan
See my updated answer for the link
Thomas Levesque
Hi Thomas. Thanks for going through all this trouble. I added another line to CustomerBindingNavigatorSaveItem_Click event handler: CustomerBindingSource.MoveFirst(). It seems that binding is not aware of the value you entered into the control until the control loses focus. I was getting NULL value exceptions from the DB on insert and that fixed the problem. I got a feeling VS 2010 is still quirky; it’s beta so I guess it’s understandable.
Dan
By default the binding updates the source when the control is validated (which happens, for instance, when it looses focus). You can change the properties of the bindings so that the source is immediately updated (PropertyChanged)
Thomas Levesque