共用方式為


Extends Attribute (EntityContainer CSDL)

The Extends attribute of the EntityContainer element in the Entity Data Model (EDM) provides a means to add entities and associations to an existing schema.

Consider a scenario where a developer defines model A. Another developer wants to extend model A to create a new model B. This new model B introduces some new types and associations between the new types and the existing types, but the changes to the original model A are only additive associations or specialized implementations of the original types.

The following schema defines a model A that includes Car and Person entities, an association between the two entities to relate car owners, and entity sets corresponding to each definition of Car, Person, and Owner.

<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="MyCompany.MySchema" Alias="ModelA" xmlns="https://schemas.microsoft.com/ado/2006/04/edm">
  <EntityContainer Name="ContainerA">
    <EntitySet Name="Cars" EntityType="ModelA.Car" />
    <EntitySet Name="People" EntityType="ModelA.Person" />
    <AssociationSet Name="CarOwners" AssociationType="ModelA.CarOwner">
      <End Role="Cars" EntitySet="ModelA.Cars" />
      <End Role="Owner" EntitySet=" ModelA.People" />
    </AssociationSet>
  </EntityContainer>

  <EntityType Name="Car">
    <Key>
      <PropertyRef Name="Make" />
      <PropertyRef Name="Model" />
    </Key>
    <Property Name="Make" Type="String" />
    <Property Name="Model" Type="String" />
  </EntityType>
  
  <EntityType Name="Person">
    <Key>
      <PropertyRef Name="FirstName" />
      <PropertyRef Name="LastName" />
    </Key>
    <Property Name="FirstName" Type="String" />
    <Property Name="LastName" Type="String" />
  </EntityType>
  
  <Association Name="CarOwner">
    <End Role="Cars" Type=" ModelA.Car" Multiplicity="*" />
    <End Role="Owner" Type=" ModelA.Owner" Multiplicity="1" />
  </Association>
</Schema>

A second developer extends the model by adding RichPerson type, Dealer type, and LuxuryCar type with corresponding entity sets. A relationship between Dealer and Car is also added to represent dealerships.

<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="MyCompany.MySchema2" Alias="ModelB" xmlns="https://schemas.microsoft.com/ado/2006/04/edm">
  <Using Namespace="MyCompany.MySchema" Alias="ModelA" />
  <EntityContainer Name="ContainerB" Extends="ContainerA" >
    <EntitySet Name="LuxuryCars" EntityType="ModelB.LuxuryCar" />
    <EntitySet Name="RichPeople" EntityType="ModelB.RichPerson" />
    <EntitySet Name="CarDealers" EntityType="ModelB.CarDealer" />
    <AssociationSet Name="DealerCars" AssociationType="ModelB.DealerCars">
      <End Role="CarDealers" EntitySet="CarDealers" />
      <End Role="Cars" EntitySet="Cars" />
    </AssociationSet>
  </EntityContainer>

  <EntityType Name="Dealer">
      …
  </EntityType>

  <EntityType Name="LuxuryCar" BaseType="ModelA.Car" >
      <Property Name="Price" Type="Decimal" />
  </EntityType>

  <EntityType Name="RichPerson" BaseType="Person">
    <Property Name="NetWorth" Type="Decimal" />
  </EntityType>

  <Association Name="DealerCars">
    <End Role="CarDealers" Type="ModelB.CarDealers" Multiplicity="1" />
    <End Role="Cars" Type="ModelA.Car" Multiplicity="*" />
  </Association>
</Schema>

See Also

Concepts

Inheritance (EDM)