Adding Entities to an Entity Data Model

This topic demonstrates how to add entities to your Entity Data Model from an existing database.

  1. In the Solution Explorer, double-click your Entity Data Model's .edmx file to open it in the design surface (if it's not already open).

  2. Right-click the Entity Data Model in the design surface and select Update Model from Database... from the shortcut menu. This launches the Entity Data Model Wizard:

  3. In the wizard's Choose Your Data Connection page, select the data connection that should be used to connect to your database. The combo box at the top of the dialog allows you to select an existing Visual Studio data connection, or you can click New Connection... to create a new connection.

    The read-only text box in the middle of the dialog displays the connection string that will be used to connect to the database. Leave the Save entity connection settings in App.Config as checkbox selected. Click Next to continue.

  4. At this point, the Entity Data Model wizard connects to your database and lists all Tables, Views, and Stored Procedures in the Add tab:


    You can choose to generate entities from a Table, View or Stored Procedure. You should choose Tables if one table in your database contains all the information for a specific business entity (for example, all the information for a Product). You should choose Views or Stored Procedures if the information for the business entity is spread out among multiple tables due to database normalization. In this example, we will select a few tables.

  5. Expand the Tables node and select one or more tables from which you want to generate entities. For example:


    Note: If you have decided to generate entities from a database that contains relationships and you want to eventually add other related tables (tables that are pointed to by foreign keys in the table you are selecting) it's best to add them all at once. This is because the Entity Data Model Wizard models related tables differently depending on whether you add them one at a time or all at once. If you add related tables at the same time, the wizard does not add a discrete foreign key property to the resulting entity object. Instead, it adds a relationship property that points to the related entity. For example, if you add the Orders table by itself, the resulting Orders entity has two foreign key pointer properties--one for EmployeeID and one for ShipVia. However, if you add Orders, Employees and Shippers all at once, the wizard produces an Orders entity that has two navigation properties that point to an Employees entity and a Shippers entity (as shown in the next step) rather than a discrete EmployeeID and ShipVia foreign key property.


  6. Click Finish, and the wizard will add an entity to the diagram for each of the selected tables (you can rearrange them on the design surface at will):

  7. You can now change the name of the entities to something more suitable. It's an MM .NET convention to name the entity with a singular noun and an "Entity" suffix. For example, you can rename "Orders" to OrderEntity. To rename an entity on the diagram, you can double-click the name of the entity in the model itself, or you can select the entity and change its name in the Properties panel. Note that as soon as you change the entity's Name property, Visual Studio automatically renames the entity's Entity Set Name property to the same name plus a "Set" extension. For example, EmployeeEntitySet.

    Here is a screen shot that shows all entities renamed using the MM .NET convention:


  8. Note: If you are converting a "classic" MM .NET Entity to an Entity Framework entity, you may want to wait to rebuild your project since there will be many compiler errors. This is because you now have two entity classes with the same name and the same properties declared in them. To see what to do next in this situation, check out the Help topic Moving a Classic Entity Business Object to Entity Framework.


    You can now rebuild your project, or you can right-click the Entity Data Model's .edmx file in Solution Explorer and select Run Custom Tool from the shortcut menu. When you do this, Visual Studio generates entity classes into the associated .cs/.vb code file from the entities on your Entity Data Model.

    The generated entity classes are stored in the EDM's associated .cs/vb file which is nested beneath the Entity Data Model's T4 template (tt) file. They are partial classes that are subclasses of your project's ABusinessEntity.

See Also:
Adding, Refreshing and Deleting Entities on an Entity Data Model


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 05/22/17
Comment or report problem with topic