本主題將示範如何手動建立具有一類一表 (Table-Per-Type) 繼承階層的概念模型。 一類一表 (Table-Per-Type) 繼承會在資料庫中使用個別資料表來維護繼承階層中每一個類型的非繼承屬性和索引鍵屬性。
注意: |
|---|
| 建議您使用 ADO.NET Entity Data Model Tools來定義具有一類一表 (Table-Per-Type) 繼承的模型。如需詳細資訊,請參閱Walkthrough: Mapping Inheritance - Table-per-Type。 |
如果要手動定義具有一類一表 (Table-Per-Type) 繼承的模型,基本步驟如下:
在概念模型中定義一個實體集,其中包含基底實體類型和衍生類型。 如需詳細資訊,請參閱 EntitySet 項目 (CSDL)。
使用 BaseType 屬性來定義概念模型中的衍生實體類型,並且針對衍生類型單獨定義非繼承屬性。 如需詳細資訊,請參閱 EntityType 項目 (CSDL)。
使用對應規格語言 (MSL),在相同的 EntitySetMapping 項目中對應基底實體類型和衍生類型。 將繼承屬性對應至資料表資料行 (如果適用的話)。 設定 TypeName 屬性的值時,請使用
IsTypeOf語法。 如需詳細資訊,請參閱 EntitySetMapping 項目 (MSL)。
下列範例假設您已經安裝 School 範例資料庫,而且已經手動設定您的專案使用 Entity Framework 。 如需詳細資訊,請參閱建立 School 範例資料庫 (Entity Framework 快速入門)和設定 Entity Framework (Entity Framework 工作)。
若要建立儲存體模型
將下列 XML 檔案加入至專案,並將它命名為
School.ssdl。<?xml version="1.0" encoding="utf-8" ?> <Schema Namespace="SchoolModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="https://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="https://schemas.microsoft.com/ado/2009/02/edm/ssdl"> <EntityContainer Name="SchoolModelStoreContainer"> <EntitySet Name="Course" EntityType="SchoolModel.Store.Course" store:Type="Tables" Schema="dbo" /> <EntitySet Name="OnlineCourse" EntityType="SchoolModel.Store.OnlineCourse" store:Type="Tables" Schema="dbo" /> <EntitySet Name="OnsiteCourse" EntityType="SchoolModel.Store.OnsiteCourse" store:Type="Tables" Schema="dbo" /> </EntityContainer> <EntityType Name="Course"> <Key> <PropertyRef Name="CourseID" /> </Key> <Property Name="CourseID" Type="int" Nullable="false" /> <Property Name="Title" Type="nvarchar" Nullable="false" MaxLength="100" /> <Property Name="Credits" Type="int" Nullable="false" /> <Property Name="DepartmentID" Type="int" Nullable="false" /> </EntityType> <EntityType Name="OnlineCourse"> <Key> <PropertyRef Name="CourseID" /> </Key> <Property Name="CourseID" Type="int" Nullable="false" /> <Property Name="URL" Type="nvarchar" Nullable="false" MaxLength="100" /> </EntityType> <EntityType Name="OnsiteCourse"> <Key> <PropertyRef Name="CourseID" /> </Key> <Property Name="CourseID" Type="int" Nullable="false" /> <Property Name="Location" Type="nvarchar" Nullable="false" MaxLength="50" /> <Property Name="Days" Type="nvarchar" Nullable="false" MaxLength="50" /> <Property Name="Time" Type="smalldatetime" Nullable="false" /> </EntityType> </Schema>
若要建立概念模型
將下列 XML 檔案加入至專案,並將它命名為
School.csdl。 請注意以下各點:僅針對三個實體類型定義一個實體集 Courses:Course、OnlineCourse 和 OnsiteCourse。
OnlineCourse 和 OnsiteCourse 實體類型是衍生類型,如其定義中的
BaseType屬性所表示。針對 OnlineCourse 和 OnsiteCourse 實體類型定義的屬性只有非繼承屬性。
<?xml version="1.0" encoding="utf-8" ?> <Schema Namespace="SchoolModel" Alias="Self" xmlns:annotation="https://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="https://schemas.microsoft.com/ado/2008/09/edm"> <EntityContainer Name="SchoolEntities" annotation:LazyLoadingEnabled="true"> <EntitySet Name="Courses" EntityType="SchoolModel.Course" /> </EntityContainer> <EntityType Name="Course"> <Key> <PropertyRef Name="CourseID" /> </Key> <Property Name="CourseID" Type="Int32" Nullable="false" /> <Property Name="Title" Type="String" Nullable="false" MaxLength="100" Unicode="true" FixedLength="false" /> <Property Name="Credits" Type="Int32" Nullable="false" /> <Property Name="DepartmentID" Type="Int32" Nullable="false" /> </EntityType> <EntityType Name="OnlineCourse" BaseType="SchoolModel.Course"> <Property Name="URL" Type="String" Nullable="false" MaxLength="100" Unicode="true" FixedLength="false" /> </EntityType> <EntityType Name="OnsiteCourse" BaseType="SchoolModel.Course"> <Property Name="Location" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" /> <Property Name="Days" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" /> <Property Name="Time" Type="DateTime" Nullable="false" /> </EntityType> </Schema>
若要定義概念模型與儲存體模型之間的對應
將下列 XML 檔案加入至專案,並將它命名為
School.msl。 請注意以下各點:Course、OnlineCourse 和 OnsiteCourse 實體類型的對應會定義在相同的 EntitySetMapping 項目中。
OnlineCourse 和 OnsiteCourse 的 CourseID 繼承屬性會對應至基礎資料庫資料表的對應資料行。
對於每個實體類型對應而言,
IsTypeOf語法是用來指出所對應的實體類型。
<?xml version="1.0" encoding="utf-8" ?> <Mapping Space="C-S" xmlns="https://schemas.microsoft.com/ado/2008/09/mapping/cs"> <EntityContainerMapping StorageEntityContainer="SchoolModelStoreContainer" CdmEntityContainer="SchoolEntities"> <EntitySetMapping Name="Courses"> <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Course)"> <MappingFragment StoreEntitySet="Course"> <ScalarProperty Name="CourseID" ColumnName="CourseID" /> <ScalarProperty Name="Title" ColumnName="Title" /> <ScalarProperty Name="Credits" ColumnName="Credits" /> <ScalarProperty Name="DepartmentID" ColumnName="DepartmentID" /> </MappingFragment> </EntityTypeMapping> <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.OnlineCourse)"> <MappingFragment StoreEntitySet="OnlineCourse"> <ScalarProperty Name="CourseID" ColumnName="CourseID" /> <ScalarProperty Name="URL" ColumnName="URL" /> </MappingFragment> </EntityTypeMapping> <EntityTypeMapping TypeName="IsTypeOf(SchoolModel.OnsiteCourse)"> <MappingFragment StoreEntitySet="OnsiteCourse"> <ScalarProperty Name="CourseID" ColumnName="CourseID" /> <ScalarProperty Name="Location" ColumnName="Location" /> <ScalarProperty Name="Days" ColumnName="Days" /> <ScalarProperty Name="Time" ColumnName="Time" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> </EntityContainerMapping> </Mapping>
另請參閱
工作
HOW TO:使用每個階層的資料表繼承來定義模型 (Entity Framework)
其他資源
CSDL、SSDL 和 MSL 規格
Entity Data Model: Inheritance
定義進階資料模型 (Entity Framework 工作)
注意: