Up Create the entity data model
Model » Create the entity data model » Review the connection string

Review the connection string

Last modified on August 15, 2012 17:21

A "Data First" Entity Data Model (EDM) is related to a database by an Entity Framework connection string. This topic shows you how to find and review the Entity Framework connection string.


Visible in the designer

The EDM Designer shows the Entity Framework connection string in the "Properties" window:

EdmProperty-connection-string.png

Hover the mouse over the "Connection string" value and it appears as a tool-tip.

Note the "Metadata Artifact Processing" value indicates that the CSDL, SSDL, and MSL files are embedded as resources in the project assembly. 

Stored in a config file

The connection string is not stored in the EDMX. It is held in a .NET configuration file. The Entity Framework designer writes the connection string to the Web.config if model is created in the web application project; it creates and writes the string to an  App.config if the model is created in its own project.

When deploying as a web application (e.g., as a Silverlight application), you must copy the <connectionStrings> section from the App.config to the Web.config.

The XML for the connection string is the same in either file and might look something like this:

XML
<connectionStrings>
    <add
      name="NorthwindIBEntities"

      connectionString="
             metadata=res://*/DomainModel.csdl|res://*/DomainModel.ssdl|res://*/DomainModel.msl;
             provider=System.Data.SqlClient;
             provider connection string=
                &quot;
                    data source=localhost;
                                initial catalog=NorthwindIB;
                                integrated security=True;
                                multipleactiveresultsets=True;
                                App=EntityFramework
                &quot;"


      providerName="System.Data.EntityClient" />
  </connectionStrings>
The entire string must appear on a single long line. It's been wrapped on this page for legibility.

The config XML <connectionString> has three parts:

  • the name of the string within the configuration file
  • the Entity Framework connection string
  • the Entity Framework provider that interprets the string.

The name of this connection string, "NorthwindIBEntities", matches the DevForce data source key name for the model. DevForce writes this data source key name into every entity class generated from the model. That's how the DevForce runtime knows which database holds entities of this type.

The inner Entity Framework connection string has parts of its own:

  • The location of the metadata artifact files
  • The database provider to use for persistence operations (here it is SQL Server)
  • The database connection string (a string within a string)

The "metadata" fragment tells us the names of the metadata artifact files. The "res://*/" segment means these files are embedded as resources in the model assembly; there would be a file path name if the artifact files were loose in the output folder.

The root name, "DomainModel" in this example, must match the EDMX file name

If you rename the EDMX file, be sure to update the names of the metadata artifact files in this string. The Entity Framework won't be able to find the artifact files if the connection string has the old names. 

Created by DevForce on March 20, 2011 13:38

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade