Complex Type Inheritance in OData v4 with ASP.NET Web API

by Microsoft

According to the OData v4 specification, a complex type can inherit from another complex type. (A complex type is a structured type without a key.) Web API OData 5.3 supports complex type inheritance.

This topic shows how to build an entity data model (EDM) with complex inheritance types.

Software versions used in the tutorial

  • Web API OData 5.3
  • OData v4

Model Hierarchy

To illustrate complex type inheritance, we'll use the following class hierarchy.

Diagram of the model hierarchy, illustrating the type inheritance for the class 'window' with arrows pointing to its children.

Shape is an abstract complex type. Rectangle, Triangle, and Circle are complex types derived from Shape, and RoundRectangle derives from Rectangle. Window is an entity type and contains a Shape instance.

Here are the CLR classes that define these types.

public class Window
    public int Id { get; set; }
    public string Title { get; set; }
    public Shape Shape { get; set; }

public abstract class Shape
    public bool HasBorder { get; set; }
    public Color Color { get; set; }

public class Rectangle : Shape
    public Point LeftTop { get; set; }
    public int Height { get; set; }
    public int Weight { get; set; }

public class RoundRectangle : Rectangle
    public double Round { get; set; }

public class Triangle : Shape
    public Point P1 { get; set; }
    public Point P2 { get; set; }
    public Point P3 { get; set; }

public class Circle : Shape
    public Point Center { get; set; }
    public int Radius { get; set; }

public class Point
    public int X { get; set; }
    public int Y { get; set; }

public enum Color

Build the EDM Model

To create the EDM, you can use ODataConventionModelBuilder, which infers the inheritance relationships from the CLR types.

private IEdmModel GetEdmModel()
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    return builder.GetEdmModel();

You can also build the EDM explicitly, using ODataModelBuilder. This takes more code, but gives you more control over the EDM.

private IEdmModel GetExplicitEdmModel()
  ODataModelBuilder builder = new ODataModelBuilder();

  EnumTypeConfiguration<Color> color = builder.EnumType<Color>();

  ComplexTypeConfiguration<Point> point = builder.ComplexType<Point>();
  point.Property(c => c.X);
  point.Property(c => c.Y);

  ComplexTypeConfiguration<Shape> shape = builder.ComplexType<Shape>();
  shape.EnumProperty(c => c.Color);
  shape.Property(c => c.HasBorder);

  ComplexTypeConfiguration<Triangle> triangle = builder.ComplexType<Triangle>();
    triangle.ComplexProperty(c => c.P1);
    triangle.ComplexProperty(c => c.P2);
    triangle.ComplexProperty(c => c.P2);

    ComplexTypeConfiguration<Rectangle> rectangle = builder.ComplexType<Rectangle>();
    rectangle.ComplexProperty(c => c.LeftTop);
    rectangle.Property(c => c.Height);
    rectangle.Property(c => c.Weight);

  ComplexTypeConfiguration<RoundRectangle> roundRectangle = builder.ComplexType<RoundRectangle>();
    roundRectangle.Property(c => c.Round);

    ComplexTypeConfiguration<Circle> circle = builder.ComplexType<Circle>();
    circle.ComplexProperty(c => c.Center);
    circle.Property(c => c.Radius);

    EntityTypeConfiguration<Window> window = builder.EntityType<Window>();
    window.HasKey(c => c.Id);
    window.Property(c => c.Title);
    window.ComplexProperty(c => c.Shape);

    return builder.GetEdmModel();

These two examples create the same EDM schema.

Metadata Document

Here is the OData metadata document, showing complex type inheritance.

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="">
    <Schema Namespace="NS" xmlns="">
      <EntityType Name="Window">
          <PropertyRef Name="Id" />
        <Property Name="Id" Type="Edm.Int32" Nullable="false" />
        <Property Name="Title" Type="Edm.String" />
        <Property Name="Shape" Type="BookStore.Shape" />
      <ComplexType Name="Shape" Abstract="true">
        <Property Name="HasBorder" Type="Edm.Boolean" Nullable="false" />
        <Property Name="Color" Type="BookStore.Color" Nullable="false" />
      <ComplexType Name="Circle" BaseType="BookStore.Shape">
        <Property Name="Center" Type="BookStore.Point" />
        <Property Name="Radius" Type="Edm.Int32" Nullable="false" />
      <ComplexType Name="Point">
        <Property Name="X" Type="Edm.Int32" Nullable="false" />
        <Property Name="Y" Type="Edm.Int32" Nullable="false" />
      <ComplexType Name="Rectangle" BaseType="BookStore.Shape">
        <Property Name="LeftTop" Type="BookStore.Point" />
        <Property Name="Height" Type="Edm.Int32" Nullable="false" />
        <Property Name="Weight" Type="Edm.Int32" Nullable="false" />
      <ComplexType Name="RoundRectangle" BaseType="BookStore.Rectangle">
        <Property Name="Round" Type="Edm.Double" Nullable="false" />
      <ComplexType Name="Triangle" BaseType="BookStore.Shape">
        <Property Name="P1" Type="BookStore.Point" />
        <Property Name="P2" Type="BookStore.Point" />
        <Property Name="P3" Type="BookStore.Point" />
      <EnumType Name="Color">
        <Member Name="Red" Value="0" />
        <Member Name="Blue" Value="1" />
        <Member Name="Green" Value="2" />
        <Member Name="Yellow" Value="3" />
    <Schema Namespace="Default" xmlns="">
      <EntityContainer Name="Container">
        <EntitySet Name="Windows" EntityType="BookStore.Window" />

From the metadata document, you can see that:

  • The Shape complex type is abstract.
  • The Rectangle, Triangle, and Circle complex type have the base type Shape.
  • The RoundRectangle type has the base type Rectangle.

Casting Complex Types

Casting on complex types is now supported. For example, the following query casts a Shape to a Rectangle.

GET ~/odata/Windows(1)/Shape/NS.Rectangle/LeftTop

Here's the response payload: