Extend model with additional property (Angular)

This guide demonstrates how to extend model with additional property.

Quick video

Source Code

You can extend your table class with additional, not-mapped properties. For example you can add ExtendedPrice to Northwind OrderDetails (SQL Table) from OrderDetailsExtended (SQL View) using partial classes:

1. Add Northwind OrderDetails partial class with ExtendedPrice property


public partial class OrderDetail
  // Additional property
  public decimal? ExtendedPrice


Public Partial Class OrderDetail
    Public Property ExtendedPrice As Decimal?
End Class

2. Populate ExtendedPrice from OrderDetailsExtended using OnOrderDetailsRead partial method in OrderDetailsController partial class


public partial class OrderDetailsController
    partial void OnOrderDetailsRead(ref IQueryable<Models.Northwind.OrderDetail> items)
        var orderDetailsExtended = this.context.OrderDetailsExtendeds.ToList();
        // Populate additional property
        foreach (var item in items)
            var orderDetailExtended = orderDetailsExtended
                .Where(ode => ode.OrderID == item.OrderID && ode.ProductID == item.ProductID)

            if (orderDetailExtended != null)
                item.ExtendedPrice = orderDetailExtended.ExtendedPrice;


Public Partial Class OrderDetailsController
    Private Partial Sub OnOrderDetailsRead(ByRef items As IQueryable(Of Models.Northwind.OrderDetail))
        Dim orderDetailsExtended = Me.context.OrderDetailsExtendeds.ToList()

        For Each item In items
            Dim orderDetailExtended = orderDetailsExtended.Where(Function(ode) ode.OrderID Is item.OrderID AndAlso ode.ProductID Is item.ProductID).FirstOrDefault()

            If orderDetailExtended IsNot Nothing Then
                item.ExtendedPrice = orderDetailExtended.ExtendedPrice
            End If
    End Sub
End Class

3. Add NorthwindContext partial class and define composite key for OrderDetailsExtended using OnOrderDetailsRead partial method to hint Entity Framework how to return unique data from SQL View


public partial class NorthwindContext
    partial void OnModelBuilding(ModelBuilder builder)
        builder.Entity<DataContextAdditionalConfiguration.Models.Northwind.OrderDetailsExtended>().HasKey(table => new


Public Partial Class NorthwindContext
    Private Partial Sub OnModelBuilding(ByVal builder As ModelBuilder)
        builder.Entity(Of DataContextAdditionalConfiguration.Models.Northwind.OrderDetailsExtended)().HasKey(Function(table) New With {table.OrderID, table.ProductID
    End Sub
End Class

4. Expose ExtendedPrice to OData using OnConfigureOData partial method in Startup partial class Startup.Custom.cs

public partial class Startup
    partial void OnConfigureOData(ODataConventionModelBuilder builder)
        // Expose additional property
        var orderDetail = builder.EntitySet<ExtendAdditionalProperty.Models.Northwind.OrderDetail>("OrderDetails");
        orderDetail.EntityType.Property(od => od.ExtendedPrice);


Public Partial Class Startup
    Private Partial Sub OnConfigureOData(ByVal builder As ODataConventionModelBuilder)
        Dim orderDetail = builder.EntitySet(Of ExtendAdditionalProperty.Models.Northwind.OrderDetail)("OrderDetails")
        orderDetail.EntityType.[Property](Function(od) od.ExtendedPrice)
    End Sub
End Class

5. Create new page, bind DataGrid component to Northwind OrderDetails and add additional column bound to ExtendedPrice property (not available in intellisense)

6. Run the application

We are here for you

Making our users happy is always a priority for us.