Note On <Programming Entity Framework 2nd Edition> -03

Chapter 8:


Install the database BreakAway


To run the .exe successfully, you need to change the name of serve to .\SQLExpress, which is (local) by default.



Create the EDM:







EDMX




The overview of the generated XML is:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>

    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>

    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>

    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
    <Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </Connection>
    <Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
        <DesignerProperty Name="EnablePluralization" Value="true" />
        <DesignerProperty Name="IncludeForeignKeysInModel" Value="true" />
        <DesignerProperty Name="UseLegacyProvider" Value="false" />
        <DesignerProperty Name="CodeGenerationStrategy" Value="None" />
      </DesignerInfoPropertySet>
    </Options>
    <!-- Diagram content (shape and connector positions) -->
    <Diagrams></Diagrams>
  </Designer>
</edmx:Edmx>


Entity


Take Customer table as example.




It's representation in StorageModels is:

<EntityType Name="Customers">
  <Key>
    <PropertyRef Name="ContactID" />
  </Key>
  <Property Name="ContactID" Type="int" Nullable="false" />
  <Property Name="CustomerTypeID" Type="int" Nullable="false" />
  <Property Name="InitialDate" Type="datetime" />
  <Property Name="PrimaryDesintation" Type="int" />
  <Property Name="SecondaryDestination" Type="int" />
  <Property Name="PrimaryActivity" Type="int" />
  <Property Name="SecondaryActivity" Type="int" />
  <Property Name="Notes" Type="varchar(max)" />
  <Property Name="RowVersion" Type="timestamp" StoreGeneratedPattern="Computed" Nullable="false" />
</EntityType>


It's representation in ConceptualModels is:

<EntityType Name="Customer">
  <Key>
    <PropertyRef Name="ContactID" />
  </Key>
  <Property Name="ContactID" Type="Int32" Nullable="false" />
  <Property Name="CustomerTypeID" Type="Int32" Nullable="false" />
  <Property Name="InitialDate" Type="DateTime" Precision="3" />
  <Property Name="PrimaryDesintation" Type="Int32" />
  <Property Name="SecondaryDestination" Type="Int32" />
  <Property Name="PrimaryActivity" Type="Int32" />
  <Property Name="SecondaryActivity" Type="Int32" />
  <Property Name="Notes" Type="String" MaxLength="Max" FixedLength="false" Unicode="false" />
  <Property Name="RowVersion" Type="Binary" MaxLength="8" FixedLength="true" Nullable="false" annotation:StoreGeneratedPattern="Computed" />
  <NavigationProperty Name="Activity" Relationship="Self.FK_Customers_Activities" FromRole="Customers" ToRole="Activities" />
  <NavigationProperty Name="Activity1" Relationship="Self.FK_Customers_Activities1" FromRole="Customers" ToRole="Activities" />
  <NavigationProperty Name="Contact" Relationship="Self.FK_Customers_Contact" FromRole="Customers" ToRole="Contact" />
  <NavigationProperty Name="CustomerType" Relationship="Self.FK_Customers_CustomerTypes" FromRole="Customers" ToRole="CustomerTypes" />
  <NavigationProperty Name="Location" Relationship="Self.FK_Customers_Locations" FromRole="Customers" ToRole="Locations" />
  <NavigationProperty Name="Location1" Relationship="Self.FK_Customers_Locations1" FromRole="Customers" ToRole="Locations" />
  <NavigationProperty Name="Reservations" Relationship="Self.FK_Reservations_Customers" FromRole="Customers" ToRole="Reservations" />
</EntityType>


This part determines how the diagram looks like in the designer view.


It's representation in Mappings is:

  <EntitySetMapping Name="Customers">
	<EntityTypeMapping TypeName="BreakAwayModel.Customer">
	  <MappingFragment StoreEntitySet="Customers">
		<ScalarProperty Name="ContactID" ColumnName="ContactID" />
		<ScalarProperty Name="CustomerTypeID" ColumnName="CustomerTypeID" />
		<ScalarProperty Name="InitialDate" ColumnName="InitialDate" />
		<ScalarProperty Name="PrimaryDesintation" ColumnName="PrimaryDesintation" />
		<ScalarProperty Name="SecondaryDestination" ColumnName="SecondaryDestination" />
		<ScalarProperty Name="PrimaryActivity" ColumnName="PrimaryActivity" />
		<ScalarProperty Name="SecondaryActivity" ColumnName="SecondaryActivity" />
		<ScalarProperty Name="Notes" ColumnName="Notes" />
		<ScalarProperty Name="RowVersion" ColumnName="RowVersion" />
	  </MappingFragment>
	</EntityTypeMapping>
  </EntitySetMapping>


Relationship


Take the association between Customers & Activities as example.






Association's representation in StorageModels is:

<Association Name="FK_Customers_Activities">
  <End Role="Activities" Type="Self.Activities" Multiplicity="0..1" />
  <End Role="Customers" Type="Self.Customers" Multiplicity="*" />
  <ReferentialConstraint>
    <Principal Role="Activities">
      <PropertyRef Name="ActivityID" />
    </Principal>
    <Dependent Role="Customers">
      <PropertyRef Name="PrimaryActivity" />
    </Dependent>
  </ReferentialConstraint>
</Association>


Association's representation in ConceptualModels is:

<Association Name="FK_Customers_Activities">
  <End Role="Activities" Type="Self.Activity" Multiplicity="0..1" />
  <End Role="Customers" Type="Self.Customer" Multiplicity="*" />
  <ReferentialConstraint>
    <Principal Role="Activities">
      <PropertyRef Name="ActivityID" />
    </Principal>
    <Dependent Role="Customers">
      <PropertyRef Name="PrimaryActivity" />
    </Dependent>
  </ReferentialConstraint>
</Association>


<AssociationSet Name="FK_Customers_Activities" Association="Self.FK_Customers_Activities">
  <End Role="Activities" EntitySet="Activities" />
  <End Role="Customers" EntitySet="Customers" />
</AssociationSet>


Association's representation in Mappings is:


Nothing. However, there is one thing worth noticing, which is if you edit the entity/property name, changing it to another name different than its corresponding database name, you will see its node in this part, like:

<AssociationSetMapping Name="ActivityEquipment" TypeName="BreakAwayModel.ActivityEquipment" StoreEntitySet="ActivityEquipment">
<EndProperty Name="Activities">
  <ScalarProperty Name="ActivityID" ColumnName="ActivityID" />
</EndProperty>
<EndProperty Name="Equipment">
  <ScalarProperty Name="EquipmentID" ColumnName="EquipmentID" />
</EndProperty>
</AssociationSetMapping>
<AssociationSetMapping Name="EventActivities" TypeName="BreakAwayModel.EventActivities" StoreEntitySet="EventActivities">
<EndProperty Name="Activities">
  <ScalarProperty Name="ActivityID" ColumnName="ActivityID" />
</EndProperty>
<EndProperty Name="Events">
  <ScalarProperty Name="TripID" ColumnName="EventID" />
</EndProperty>
</AssociationSetMapping>


You see this because you have updated Equipment entity name and Event entity name.


Navigation and association are always linked to each other:



It's representation in ConceptualModels:

<EntityType Name="Activity">
  ...
  <NavigationProperty Name="Customers" Relationship="Self.FK_Customers_Activities" FromRole="Activities" ToRole="Customers" />
  ...
</EntityType>


<EntityType Name="Customer">
  ...
  <NavigationProperty Name="Activity" Relationship="Self.FK_Customers_Activities" FromRole="Customers" ToRole="Activities" />
  ...
</EntityType>


Now, let's follow the instruction at page 171:

Old property nameNew property name
Customer.PrimaryDestinationCustomer.PrimaryDestinationID


What really happened behind the scene?


In the ConceptualModels part:


before changed

<Schema Namespace="BreakAwayModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
  <EntityType Name="Customer">
    ..
    <Property Name="PrimaryDesintation" Type="Int32" />
    ..
  </EntityType>
  ..
  <Association Name="FK_Customers_Locations">
    ..
    <ReferentialConstraint>
      <Principal Role="Locations">
        <PropertyRef Name="DestinationID" />
      </Principal>
      <Dependent Role="Customers">
        <PropertyRef Name="PrimaryDesintation" />
      </Dependent>
    </ReferentialConstraint>
  </Association>

</Schema>

after changed:

<Schema Namespace="BreakAwayModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
  <EntityType Name="Customer">
    ..
    <Property Name="PrimaryDesintation" Type="Int32" />
    ..
  </EntityType>
  ..
  <Association Name="FK_Customers_Locations">
    ..
    <ReferentialConstraint>
      <Principal Role="Locations">
        <PropertyRef Name="DestinationID" />
      </Principal>
      <Dependent Role="Customers">
        <PropertyRef Name="PrimaryDesintationID" />
      </Dependent>
    </ReferentialConstraint>
  </Association>

</Schema>


In Mapping part:

<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
..
  <EntityContainerMapping StorageEntityContainer="BreakAwayModelStoreContainer" CdmEntityContainer="BreakAwayEntities">
      <EntitySetMapping Name="Customers">
        <EntityTypeMapping TypeName="BreakAwayModel.Customer">
          <MappingFragment StoreEntitySet="Customers">
            ..
            <ScalarProperty Name="PrimaryDesintation" ColumnName="PrimaryDesintation" />
            ..
          </MappingFragment>
        </EntityTypeMapping>
      </EntitySetMapping>
  </EntityContainerMapping>
..


In Customer.cs:

before:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace BreakAwayModel
{
    using System;
    using System.Collections.Generic;
    
    public partial class Customer
    {
        ...
    
        ...
        public Nullable<int> PrimaryDesintation { get; set; }
        ...
    
        ...
    }
}


after:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace BreakAwayModel
{
    using System;
    using System.Collections.Generic;
    
    public partial class Customer
    {
        ...
    
        ...
        public Nullable<int> PrimaryDesintationID { get; set; }
        ...
    
        ...
    }
}



Updating the navigation property


before:

<Schema Namespace="BreakAwayModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
  <EntityType Name="Customer">
    <Key>
      <PropertyRef Name="ContactID" />
    </Key>
    <Property Name="ContactID" Type="Int32" Nullable="false" />
    ..
    <NavigationProperty Name="Activity" Relationship="Self.FK_Customers_Activities" FromRole="Customers" ToRole="Activities" />
    <NavigationProperty Name="Activity1" Relationship="Self.FK_Customers_Activities1" FromRole="Customers" ToRole="Activities" />
    ..
  </EntityType>
  ..


after:

<Schema Namespace="BreakAwayModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
  <EntityType Name="Customer">
    <Key>
      <PropertyRef Name="ContactID" />
    </Key>
    <Property Name="ContactID" Type="Int32" Nullable="false" />
    ..
    <NavigationProperty Name="PrimaryActivity" Relationship="Self.FK_Customers_Activities" FromRole="Customers" ToRole="Activities" />
    <NavigationProperty Name="SecondaryActivity" Relationship="Self.FK_Customers_Activities1" FromRole="Customers" ToRole="Activities" />
    ..
  </EntityType>
  ..













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值