Database Reference
In-Depth Information
The following is the output of the code in Listing 6-37:
Active Invoice
Description: Oil Change
Amount: $19.95
Date: 4/11/2013
Active Invoice
Description: Wheel Alignment
Amount: $129.95
Date: 4/1/2013
Deleted Invoice
Description: Engine Diagnosis
Amount: $39.95
Date: 4/1/2013
6-14. Creating Independent and Foreign Key Associations
Problem
You want to use Model First to create both independent and foreign key associations.
Solution
Foreign keys and independent associations help us maintain referential integrity within the database schema and
provide navigation paths to related entities. In order to create foreign keys and independent associations using Model
First, do the following:
1.
Add a new ADO.NET Entity Data Model to your project. Select Empty Model when
prompted to choose the model contents. Click Finish. This will create an empty design
surface.
2.
Right-click the design surface, and select Add Entity. Name the new entity User and
click OK.
3.
Right-click the new entity, and add a scalar property for the UserName.
4.
Right-click the design surface, and select Add Entity. Name the new entity
PasswordHistory and click OK.
5.
Right-click the new entity, and add a scalar property for the LastLogin. Right-click the
LastLogin property, and change its type to DateTime.
6.
Right-click the User entity, and select Add Association. To create a foreign key
association, check the Add foreign key properties to the PasswordHistory entity check box.
To create an independent association, uncheck this box.
7.
Right-click the design surface, and select Generate Model from Database. Select a
database connection, and complete the remainder of the wizard. This will generate the
storage and mapping layers of the model and produce a script to generate the database
for the model.
 
 
Search WWH ::




Custom Search