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

50 篇文章 0 订阅
15 篇文章 0 订阅

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>
  ..













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
英文高清原版,本人资源全部免费,更多资源请查看本人的上传资源 =================================================== 作 者:Julia Lerman 页 数:914页 定 价:567.00 内 容 简 介:Get a thorough introduction to ADO.NET Entity Framework 4 --Microsoft's core framework for modeling and interacting with datain .NET applications. The second edition of this acclaimed guideprovides a hands-on tour of the framework latest version in Visual Studio 2010 and .NET Framework 4. Not only will you learn how touse EF4 in a variety of applications, you'll also gain a deepunderstanding of its architecture and APIs. Written by JuliaLerman, the leading independent authority on the framework,Programming Entity Framework covers it all -- from the Entity DataModel and Object Services to WCF Services, MVC Apps, and unittesting. This book highlights important changes for experienced developers familiar with the earlier version. * Understand the coreconcepts you need to make the best use of the EF4 in your applications * Learn to query your data, using either LINQ toEntities or Entity SQL * Create Windows Forms, WPF, ASP.NET WebForms, and ASP.NET MVC applications * Build and consume WCF Services, WCF Data Services, and WCF RIA Services * Use Object Services to work directly with your entity objects * Createpersistent ignorant entities, repositories, and write unit tests *Delve into model customization, relationship management, change tracking, data concurrency, and more * Get scores of reusable examples -- written in C# (with notes on Visual Basic syntax) --that you can implement right away 作 者 简 介:Julia Lerman is the leading independent authority on the Entity Framework and has been using and teaching the technology since itsinception two years ago. She is well known in the .NET community asa Microsoft MVP, ASPInsider and INETA Speaker. She is a prolific blogger, a frequent presenter at technical conferences around the world, including DevConnections and TechEd and she writes articlesfor many well-known technical publications. Julia lives in Vermontwhere she runs the Vermont.NET User Group, is a board member of theVermont Software Developers Alliance, and a member of the Champlain College Software Engineering Advisory Board. You can read her blogsat www.thedatafarm.com/blog.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值