Поделиться через


How to: Use a Model Defined in a Class Library

This topic describes how to use an Entity Data Model (EDM) in a project when the EDM is defined in a Visual Studio 2008 Class Library project.

The first procedure in this topic describes how to use an EDM that is defined in a Class Library from projects other than ASP.NET Website projects. The second procedure describes how to use an EDM that is defined in a Class Library from an ASP.NET Website project.

To use an EDM defined in a Class Library from projects other than ASP.NET Website projects

  1. In Visual Studio, create a Visual C# or Visual Basic Class Library project.

    For information about creating projects in Visual Studio, see How to: Create Solutions and Projects.

  2. Add an EDM to the Class Library project.

    For information about creating a new EDM and adding it to a project, see How to: Create a New Entity Data Model. For information about adding an existing EDM to a project, see How to: Add an Existing Entity Data Model.

  3. Add a new project that is not an ASP.NET Website project to your existing solution.

    For information about adding a project to an existing solution, see How to: Create Multi-Project Solutions.

  4. Right-click the newly added project (not the Class Library project) and select Add Reference.

    The Add Reference dialog box appears.

  5. Click the Projects tab of the Add Reference dialog box and select your Class Library project. Click OK.

  6. Again, right-click the newly added project (not the Class Library project) and select Add Reference.

  7. Click the .NET tab of the Add Reference dialog box and select System.Data.Entity. Click OK.

  8. Open the App.Config file of the Class Library Project.

  9. Highlight the connection string section of the App.Config file, right-click it, and select Copy. For example, copy the code that looks like this:

    <add name="SchoolEntities" 
               connectionString="metadata=res://*/School.csdl|
                                          res://*/School.ssdl|
                                          res://*/School.msl;
                                 provider=System.Data.SqlClient;
                                 provider connection string=&quot;
                                 Data Source=(local);
                                 Initial Catalog=School;
                                 Integrated Security=True;
                                 MultipleActiveResultSets=True&quot;" 
               providerName="System.Data.EntityClient" />
    
  10. Open the App.Config file of the newly added project and paste the copied connection string information into the <connectionStrings> node.

    Note

    If the project does not have an App.Config file, you can add one by right-clicking the project in Solution Explorer, pointing to Add, and selecting New Item. Choose Application Configuration File from the dialog box that opens and click OK.

You can now write code in the newly added project that will use the EDM that is defined in the Class Library project.

Note

You might have to set the newly added project as the startup project before building the solution. To do this, right click the project in Solution Explorer and select Set as StartUp Project.

To use an EDM defined in a Class Library from an ASP.NET Website project

  1. In Visual Studio, create a Visual C# or Visual Basic Class Library project.

    For information about creating projects in Visual Studio, see How to: Create Solutions and Projects.

  2. Add an EDM to the Class Library project.

    For information about creating a new EDM and adding it to a project, see How to: Create a New Entity Data Model. For information about adding an existing EDM to a project, see How to: Add an Existing Entity Data Model.

  3. Add a an ASP.NET Website project to your existing solution.

    For information about adding a project to an existing solution, see How to: Create Multi-Project Solutions.

  4. Right-click the ASP.NET Website project and select Add Reference.

    The Add Reference dialog box appears.

  5. Click the .NET tab of the Add Reference dialog box and select System.Data.Entity. Click OK.

  6. The next step is to reference the Class Library project from the ASP.NET Website project. There are two ways you can do this:

    1. Right-click the ASP.NET Website project in Solution Explorer and select Add Reference.

      The Add Reference dialog box appears.

    2. Click the Projects tab of the Add Reference dialog box and select your Class Library project. Click OK.

    -OR-

    1. In Solution Explorer, right-click the Class Library project and select Build.

      The Class Library assembly is added to the bin\Debug folder of the project.

    2. Right-click the ASP.NET Website project and select Add ASP.NET Folder. Select Bin.

      The Bin folder is added to the project.

    3. In Windows Explorer, navigate to the bin\Debug folder of the Class Library project. Right-click the project assembly (the <project_name>.dll file) and select Copy.

    4. In Solution Explorer, right-click the Bin folder under the ASP.NET Website project and select Paste.

  7. The next step is to make the model and mapping files available to the ASP.NET Website project. You can do this by embedding the model and mapping files as resources in the Class Library assembly, as described in the following steps. For alternative methods, see the procedure at the end of this topic ("To copy mapping and metadata files to an ASP.NET Website project").

    1. Open the App.Config file of the Class Library project.

    2. Highlight the connection string information, right-click, and select Copy. For example, copy the code that looks like this:

      <add name="SchoolEntities" 
                 connectionString="metadata=res://*/School.csdl|
                                            res://*/School.ssdl|
                                            res://*/School.msl;
                                   provider=System.Data.SqlClient;
                                   provider connection string=&quot;
                                   Data Source=(local);
                                   Initial Catalog=School;
                                   Integrated Security=True;
                                   MultipleActiveResultSets=True&quot;" 
                 providerName="System.Data.EntityClient" />
      
    3. Open the Web.config file of the ASP.NET Website project and paste the copied connection string information into the <connectionStrings> node.

You can now write code in the ASP.NET Website project that will use the EDM that is defined in the Class Library project.

Note

You might have to set the newly added project as the startup project before building the solution. To do this, right click the project in Solution Explorer and select Set as StartUp Project.

Copying Mapping and Metadata Files to an ASP.NET Website Project

The following procedures describe two options for copying model and mapping files to an ASP.NET Website project. Both methods described here are alternatives to step 7 in the procedure above. The first procedure describes how to copy the files to the Bin folder of an ASP.NET Website project and the second procedure describes how to copy the files to the App_Data folder. Do not embed the model and mapping files as resources in the Class Library assembly if you choose either of the these options.

To copy mapping and metadata files to the Bin folder of an ASP.NET Website project

  1. Double click the .edmx file in the Class Library project. The .edmx file opens in the ADO.NET Entity Data Model Designer (Entity Designer).

  2. Right-click an empty area of the design surface and select Properties.

  3. In the Properties window, set the value of the Metadata Artifact Processing property to Copy to Output Directory. When you select this value, the Entity Designer changes the connection string in the Class Library's App.Config file to have relative file paths.

  4. In Solution Explorer, right-click the Class Library project and select Build.

  5. In Windows Explorer, navigate to the Class Library output directory (bin\Debug folder) and copy the .csdl, .ssdl, and .msl files.

  6. Right-click on the Bin folder under the ASP.NET Website project and select Paste.

    Note

    If the project does not contain a Bin folder, you can add one by right-clicking the project in Solution Explorer, pointing to Add ASP.NET Folder, and selecting Bin.

  7. Open the App.Config file of the Class Library project. Highlight the connection string information, right-click, and select Copy.

  8. Open the Web.config file of the ASP.NET Website project and paste the copied connection string information into the <connectionStrings> node.

  9. Change the metadata entries in the connection string to be prefixed by ~\bin\. For example, the copied and modified connection string entry should look similar to this:

    <add name="SchoolEntities" 
             connectionString="metadata=~\bin\School.csdl|
                                        ~\bin\School.ssdl|
                                        ~\bin\School.msl;
                               provider=System.Data.SqlClient;
                               provider connection string=&quot;
                               Data Source=(local);
                               Initial Catalog=School;
                               Integrated Security=True;
                               MultipleActiveResultSets=True&quot;" 
             providerName="System.Data.EntityClient" />
    

To copy the model and mapping files to the App_Data folder of the ASP.NET Website

  • The steps for copying the model and mapping files to the App_Data folder are similar to the steps for copying them to the Bin folder. However, there are two key differences:

    • The .csdl, .ssdl, and .msl files should be copied to the App_Data folder of the ASP.NET Website project, instead of the Bin folder (step 6 above).

      Note   If the project does not contain a App_Data folder, you can add one by right-clicking the project in Solution Explorer, pointing to Add ASP.NET Folder, and selecting App_Data.

    • After copying the connection string information from the App.Config folder of the Class Library to the Web.config file of ASP.NET Website (steps 7 and 8 above), change the metadata entries to be prefixed by |DataDirectory|. For example, the copied and modified connection string entry should look similar to the following:

      <add name="SchoolEntities" 
               connectionString="metadata=|DataDirectory|\School.csdl|
                                          |DataDirectory|\School.ssdl|
                                          |DataDirectory|\School.msl;
                                 provider=System.Data.SqlClient;
                                 provider connection string=&quot;
                                 Data Source=(local);
                                 Initial Catalog=School;
                                 Integrated Security=True;
                                 MultipleActiveResultSets=True&quot;" 
               providerName="System.Data.EntityClient" />
      

See Also

Other Resources

Deployment Tasks with the Entity Designer
Entity Data Model Tools Tasks
ADO.NET Entity Data Model Designer Scenarios
Connection Strings (Entity Framework)