Databases Reference
In-Depth Information
<EntitySetMapping Name=”Users”>
<EntityTypeMapping TypeName=”IsTypeOf(NorthwindModel2.User)”>
<MappingFragment StoreEntitySet=”Customer”>
<ScalarProperty Name=”ContactTitle” ColumnName=”ContactTitle” />
<ScalarProperty Name=”ContactName” ColumnName=”ContactName” />
<ScalarProperty Name=”CustomerID” ColumnName=”CustomerID” />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName=”IsTypeOf(NorthwindModel2.Customer)”>
<MappingFragment StoreEntitySet=”CustomerAddress”>
<ScalarProperty Name=”Fax” ColumnName=”Fax” />
<ScalarProperty Name=”Phone” ColumnName=”Phone” />
<ScalarProperty Name=”Country” ColumnName=”Country” />
<ScalarProperty Name=”PostalCode” ColumnName=”PostalCode” />
<ScalarProperty Name=”Region” ColumnName=”Region” />
<ScalarProperty Name=”City” ColumnName=”City” />
<ScalarProperty Name=”Address” ColumnName=”Address” />
<ScalarProperty Name=”CustomerID” ColumnName=”CustomerID” />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
With these conceptual and mapping model definitions, you can easily query all users (this
returns both User and Customer entity objects):
var query = from u in context.Users
select u;
On the other hand, if we are interested only in Customers, we can use the OfType exten-
sion method, which returns only the entities of a given type:
var query = from c in context.Users. OfType<Customer> ()
select c;
Inheritance Mapping
Three different approaches to mapping inheritance hierarchies to database tables exist—
table-per-type, table-per-concrete-type, and table-per-hierarchy.
With the table-per-type approach, used in the previous example, each entity type in the
conceptual model is mapped to a separate database table. To retrieve a derived entity such
as Customer, the Entity Framework performs a SQL join of all tables storing properties of
parent entity types.
With the table-per-concrete-type approach, each nonabstract type in the inheritance hierar-
chy gets its own table with a full set of columns necessary to store properties of the entity
type itself and all of its parent types. This allows the Entity Framework to query only one
 
Search WWH ::




Custom Search