Part 1 of this article demonstrated how to create two entities
using the Entity Framework 4. The two entities are UserAccount and Address. Each
entity had an Id field defined as the primary key and various fields specific
to the entity. The one thing I forgot to show you in the first article was how
to tell the Entity Framework to define the Id field as an Identity field.
1.
Launch Visual Studio and open the OrderSystem project.
2.
Double click on the OrderDB.edmx file in the Solution Explorer to view
the Entity Framework designer.
3.
Click on the Id field in the UserAccount entity and view its properties.
4.
To define a field as an Identity field you need to change the
StoreGeneratedPattern property to Identity. Do the same for the Id field in
the Addresses entity.
Figure 2
5.
Now that the Entity Model is updated, you need to update the database.
Right click on the Designer and select Generate Database From Model. This will
bring up the Generate Database Wizard. The DDL to create the two tables with
the Identity field will be automatically generated. Click the Finish button to
create the script. You'll get the following message warning you that you are
going to overwrite the script that already exists. Click the Yes button to
generate the script.
Figure 3
6.
The OrderDB.edmx.sql file will be overwritten and should open in Visual
Studio. You now need to execute the sql file against the database. To do this
simply right click anywhere in the file and select Execute SQL from the pop-up
menu.
Figure 4
You'll be prompted to connect to the database. Once you log
in the script will execute.