Home > Sample chapters > Programming > Visual Studio and .NET

Getting to Know the Entity Framework

Introducing the Entity Framework files

As previously mentioned, all of the files used with the Entity Framework rely on XML. The use of XML makes the files portable and easy to use with other applications. You can also view the content of these files and reasonably expect to understand much of what they contain. However, each of the Entity Framework elements uses a different XML file with a different file extension and a different language inside.

After you create a new application that relies on the Entity Framework and define the required database models, you can find the resulting files in the main folder of the project. When working with Visual Studio 2012, you’ll find a single Entity Data Model XML (.EDMX) file. However, when working with older versions of Visual Studio, you may find individual files for each of the Entity Framework elements.

Providing a complete tutorial on each of these files is outside the scope of this book. The following sections provide a useful overview of the files, which you can use for further study.

Viewing the Conceptual Schema Definition Language file

The Conceptual Schema Definition Language (.CSDL) file contains the XML required to build the conceptual model of the database content as viewed by the application. You see this content in graphical format when working with Visual Studio. To see it in plain-text form, locate the .CSDL or .EDMX file for your application in the project folder. Right-click this file in Microsoft Windows Explorer and choose Open With from the context menu. Locate Notepad or some other suitable text editor in the Open With dialog box, clear any option that says that this program will become the default program for opening this file, and click OK. You’ll see the XML for the conceptual model for the application. Following is the XML for the sample application that appears later in the chapter (some <Schema> attributes are removed to make the listing easier to read).

<!-- CSDL content -->
<edmx:ConceptualModels>
  <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm"…>
    <EntityContainer Name="Model1Container" annotation:LazyLoadingEnabled="true">
      <EntitySet Name="Customers" EntityType="Model1.Customer" />
    </EntityContainer>
    <EntityType Name="Customer">
      <Key>
        <PropertyRef Name="CustomerID" />
      </Key>
      <Property Type="Int32" Name="CustomerID" Nullable="false"
                annotation:StoreGeneratedPattern="Identity" />
      <Property Type="String" Name="FirstName" Nullable="false" />
      <Property Type="String" Name="LastName" Nullable="false" />
      <Property Type="String" Name="AddressLine1" Nullable="false" />
      <Property Type="String" Name="AddressLine2" Nullable="false" />
      <Property Type="String" Name="City" Nullable="false" />
      <Property Type="String" Name="State_Province" Nullable="false" />
      <Property Type="String" Name="ZIP_Postal_Code" Nullable="false" />
      <Property Type="String" Name="Region_Country" Nullable="false" />
    </EntityType>
  </Schema>
</edmx:ConceptualModels>

The XML makes it easier to understand the preceding discussion of how an Entity object works. Notice that the XML describes an entity container, used to hold all of the entities for this particular model. Within that container is a single EntityType named Customer. As with all Entity objects, this one has a Key property named CustomerID that gives the Entity a unique value. In addition, there are a number of properties associated with this Entity, such as FirstName. You’ll see how the properties work later in the chapter. Of course, an Entity can have other elements associated with it, and you’ll see them at work later in the book.

Look at the individual <Property> entries. Each one includes a .NET type. In this case, the types are limited to Int32 and String, but you have access to a number of other types. You can see the primitive data types supported by the Entity Framework at http://msdn.microsoft.com/library/ee382832.aspx.

Viewing the Store Schema Definition Language file

The .SSDL file contains the XML required to define the storage model of the database content as viewed by the database manager. As with the conceptual model, you see the database described in terms of the entities required to create it. The entries rely on SQL data types, rather than .NET data types. Here’s an example of the XML used to create a storage model for the example that appears later in the chapter (the <Schema> has been shortened to make the text easier to read):

  <!-- SSDL content -->
  <edmx:StorageModels>
  <Schema Namespace="Model1.Store" Alias="Self"…>
<EntityContainer Name="Model1StoreContainer">
  <EntitySet Name="Customers" EntityType="Model1.Store.Customers" store:Type="Tables"
             Schema="dbo" />
</EntityContainer>
<EntityType Name="Customers">
  <Key>
    <PropertyRef Name="CustomerID" />
  </Key>
  <Property Name="CustomerID" Type="int" StoreGeneratedPattern="Identity"
            Nullable="false" />
  <Property Name="FirstName" Type="nvarchar(max)" Nullable="false" />
  <Property Name="LastName" Type="nvarchar(max)" Nullable="false" />
  <Property Name="AddressLine1" Type="nvarchar(max)" Nullable="false" />
  <Property Name="AddressLine2" Type="nvarchar(max)" Nullable="false" />
  <Property Name="City" Type="nvarchar(max)" Nullable="false" />
  <Property Name="State_Province" Type="nvarchar(max)" Nullable="false" />
  <Property Name="ZIP_Postal_Code" Type="nvarchar(max)" Nullable="false" />
  <Property Name="Region_Country" Type="nvarchar(max)" Nullable="false" />
</EntityType>

Viewing the Mapping Specification Language file

The .MSL file creates a relationship between the .CSDL and .SSDL files. The mapping serves to define how the application view and the database manager view reflect the same database, but from differing perspectives. For example, the model mapping defines which conceptual model property translates into a particular storage model property. Here’s the model-mapping content for the example that appears later in the chapter:

<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
<EntityContainerMapping StorageEntityContainer="Model1StoreContainer"
                        CdmEntityContainer="Model1Container">
  <EntitySetMapping Name="Customers">
    <EntityTypeMapping TypeName="IsTypeOf(Model1.Customer)">
      <MappingFragment StoreEntitySet="Customers">
        <ScalarProperty Name="CustomerID" ColumnName="CustomerID" />
        <ScalarProperty Name="FirstName" ColumnName="FirstName" />
        <ScalarProperty Name="LastName" ColumnName="LastName" />
        <ScalarProperty Name="AddressLine1" ColumnName="AddressLine1" />
        <ScalarProperty Name="AddressLine2" ColumnName="AddressLine2" />
        <ScalarProperty Name="City" ColumnName="City" />
        <ScalarProperty Name="State_Province" ColumnName="State_Province" />
        <ScalarProperty Name="ZIP_Postal_Code" ColumnName="ZIP_Postal_Code" />
        <ScalarProperty Name="Region_Country" ColumnName="Region_Country" />
      </MappingFragment>
    </EntityTypeMapping>
  </EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>