Sunday, April 3, 2011

Originally published on DataDeveloper.net

In this tutorial, you will create a new Entity Data Model inside an Class Library Project. The data model will be built from the AdventureWorksLT database.

The project can be used in (Windows, Console, Web, other class libraries etc) by referencing this project.

Create a new Class Library Project

  1. Select New Project from File Menu option
  2. Select your preferred project base: e.g. Visual Basic or C#
  3. Select Class Library
  4. Change the name of the application in the Name text box to DataDevEDM
  5. Click OK

Add an Entity Data Model Item

  1. In Solution Explorer, right click the project name.
  2. Click Add on the context menu that opens.
  3. Click New Item.
  4. In the Add New Item dialog, click ADO.NET Entity Data Model.
  5. Change the name from Model1.edmx to AWModel.edmx
    addnewitem
  6. Click the Add button
  7. In the Entity Data Model Wizard click Generate from Database, then click the Next button

Create a database connection for the Entity Data Model

If you have a database connection already created in Visual Studio, skip to Define Contents of an Entity Data Model

  1. On the Choose Your Data Connection page of the wizard, click the New Connection button.
  2. At the top of the connection properties window, click the Change button next to the Data Source
    clip_image004
  3. Select Microsoft SQL Server Database File from the Data source list, then click OK
  4. Click the Browse button to the right of Database file name
  5. Browse to the folder where you have saved the AdventureWorksLT database*.
  6. Select AdventureWorksLT.mdf then click Open.
    clip_image006
  7. At the bottom of the Connection Properties window, click OK

Define the contents of an Entity Data Model

  1. Select the database connection from the drop down.
  2. At the bottom of the Entity Data Model Wizard window, change the value of Entity connection settings to AWEntities then click Next.
    clip_image008

  3. On the next page, change the Model Namespace to AWModel.
  4. Check Tables, Views and Stored Procedures.
    This will include all of the items. You can expand these nodes to select only the specific tables, views and stored procedures that you want.
    clip_image010

  5. Click Finish.
  6. Note that in the solution explorer, a new file, AWModel.edmx has been created.
    explorer

View the model

  1. Double click the AWModel.emx file to open the model in the Entity Data Model designer.

model

0 Comments:

Post a Comment