Add New EntityRef and Entity Instances with Code

Listing 1. The CustomersDataGridView doesn't allow new rows by default, and the other two DataGridViews throw exceptions when you click their empty (tentative append) row. For the technical preview, you must write code that's similar to the following to add the new entity instances, then apply the DataContext.Submit method to insert the rows into the database tables.

Private Sub AddNewCustomer()
   'Create some new OrderDetails instances
   blnFromCustomers = True
   Dim objLineItem1 As New Nwind.OrderDetails
   With objLineItem1
     .ProductID = 1
     .Quantity = 12
     .UnitPrice = 22.25
     .Discount = 0.05
   End With
   …
   'Create a new Order instance    'Requires changing Orders.EmployeeID and    'Orders.ShipVia field data types from    'Nullable(Of Integer) to Integer    'Otherwise these field values remain Null,    'regardless of setting    Dim objOrder As New Nwind.Orders    With objOrder      .CustomerID = "ABOGC"      .EmployeeID = 1      .OrderDate = Now      .RequiredDate = Now.AddDays(14)      .ShippedDate = Nothing      .ShipVia = 2      .Freight = 15.15      .ShipName = "A Bogus Customer, Inc."      .ShipAddress = "1000 Broadway"      .ShipCity = "Oakland"      .ShipRegion = "CA"      .ShipPostalCode = "94608"      .ShipCountry = "USA"      .OrderDetails.Add(objLineItem1)      ,,,    End With
   'Add the new Customer instance    Dim objCustomer As New Nwind.Customers    With objCustomer      .CustomerID = "ABOGC"      .CompanyName = "A Bogus Customer, Inc."      .ContactName = "Joe Bogus"      .ContactTitle = "President and CEO"      .Address = "1000 Broadway"      .City = "Oakland"      .Region = "CA"      .PostalCode = "94608"      .Country = "USA"      .Phone = "(510) 555-1212"      .Fax = "(510) 555-1213"      .Orders.Add(objOrder)    End With    dbNwind.Customers.Add(objCustomer)    dbNwind.SubmitChanges()    txtTime.Text = Format(SWatch.ElapsedTime, "#,##0.000")    …    End Sub