views:

24

answers:

1

I am very new to the ADO Entity Framework, and I'm looking for the best practice.

Here is the scenario: I have a database with a Person table and an Address table. Person has a foreign key referencing the primary key of Address, an auto-incrementing int. First, I've created an ADO Entity model with VS 2010, and sure enough the relationship is understood by the IDE.

Now I want to create a Windows Form that allows the user to fill in a Person's basic info and Address. Normally, I can just drag the Person entity onto a blank Windows Form, and VS2010 will automatically create the necessary fields along with the binding. But what extra work do I need to do in order to ensure the following happens when this form is filled out by the user:

  1. The Address portion of the form is saved in the Address table
  2. The Person information is saved in the Person table, along with the primary key of the Address created in 1.
  3. The user never touches/sees any primary keys.

I think my confusion is coming from the fact that I am expecting the IDE to do more coding than it's supposed to; If I had to quickly hack this, I'd probably add a click event to the "save" button that first calls SaveChanges() on the Address followed by the Person. Is that the best practice, or is there a more accepted way of doing this?

A: 

You shouldn't have to do a lot extra. But you should call SaveChanges on the DataContext common to Address and Person (to correctly handle the different insert/delete scenarios).

You should simply delete all controls (columns) relating to the Keys.

Are you having a specific problem?

Henk Holterman
I'm not having a specific problem; just getting adjusted to C#/ADO/.NET.I also knew I could delete the controls relating to keys, but I was more unsure about how related entities are saved on the same form. I think I'll need to read up on DataContext, as it seems like there is more to this than I thought.
Yes, read up. There are multiple ways to create new objects too. But it is relatively easy. Don't fiddle with the keys, instead add entities to collections or set them in properties.
Henk Holterman