开发者

WinForm for creating ADO.NET Entities that have relationships with other entities

开发者 https://www.devze.com 2023-01-03 08:15 出处:网络
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 referencin

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 Wind开发者_Go百科ows 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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消