How to: Use a Model Defined in a Class Library (Entity Data Model Tools)
This topic describes how to use a conceptual model that was defined in a Visual Studio 2008 Class Library project.
The first procedure in this topic describes how to use a conceptual model that was defined in a Class Library from projects other than ASP.NET Website projects. The second procedure describes how to use a conceptual model that was defined in a Class Library from an ASP.NET Website project.
To use a model defined in a Class Library from projects other than ASP.NET Website projects
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.
Add a conceptual model to the Class Library project.
For information about creating a new conceptual model and adding it to a project, see How to: Create a New .edmx File (Entity Data Model Tools). For information about adding an existing conceptual model to a project, see How to: Add an Existing .edmx File (Entity Data Model Tools).
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.
Right-click the newly added project (not the Class Library project) and select Add Reference.
The Add Reference dialog box appears.
Click the Projects tab of the Add Reference dialog box and select your Class Library project. Click OK.
Again, right-click the newly added project (not the Class Library project) and select Add Reference.
Click the .NET tab of the Add Reference dialog box and select System.Data.Entity. Click OK.
Open the App.Config file of the Class Library Project.
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=" Data Source=(local); Initial Catalog=School; Integrated Security=True; MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
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 conceptual model 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 a model defined in a Class Library from an ASP.NET Website project
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.
Add a conceptual model to the Class Library project.
For information about creating a new conceptual model and adding it to a project, see How to: Create a New .edmx File (Entity Data Model Tools). For information about adding an existing conceptual model to a project, see How to: Add an Existing .edmx File (Entity Data Model Tools).
Add 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.
Right-click the ASP.NET Website project and select Add Reference.
The Add Reference dialog box appears.
Click the .NET tab of the Add Reference dialog box and select System.Data.Entity. Click OK.
The next step is to reference the Class Library project from the ASP.NET Website project. There are two ways you can do this:
Right-click the ASP.NET Website project in Solution Explorer and select Add Reference.
The Add Reference dialog box appears.
Click the Projects tab of the Add Reference dialog box and select your Class Library project. Click OK.
-OR-
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.
Right-click the ASP.NET Website project and select Add ASP.NET Folder. Select Bin.
The Bin folder is added to the project.
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.
In Solution Explorer, right-click the Bin folder under the ASP.NET Website project and select Paste.
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").
Open the App.Config file of the Class Library project.
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=" Data Source=(local); Initial Catalog=School; Integrated Security=True; MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
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 conceptual model 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 model and mapping files to the Bin folder of an ASP.NET Website project
Double click the .edmx file in the Class Library project. The .edmx file opens in the ADO.NET Entity Data Model Designer (Entity Designer).
Right-click an empty area of the design surface and select Properties.
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.
In Solution Explorer, right-click the Class Library project and select Build.
In Windows Explorer, navigate to the Class Library output directory (bin\Debug folder) and copy the .csdl, .ssdl, and .msl files.
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.
Open the App.Config file of the Class Library project. Highlight the connection string information, right-click, and select Copy.
Open the Web.config file of the ASP.NET Website project and paste the copied connection string information into the
<connectionStrings>
node.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=" Data Source=(local); Initial Catalog=School; Integrated Security=True; MultipleActiveResultSets=True"" 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=" Data Source=(local); Initial Catalog=School; Integrated Security=True; MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
See Also
Other Resources
Deployment Tasks with the Entity Data Model Tools
Entity Data Model Tools Tasks
Entity Data Model Tools Scenarios
Connection Strings (Entity Framework)