Save $100 with our Black Friday deals!

Get $100 OFF with promo code BLACKFRIDAY2024 at checkout until November 30.

See Pricing

Still using Radzen Studio?
Achieve more with Radzen Blazor Studio

Radzen Blazor Studio is our new flagship product and vision of how rapid Blazor application development should be done.

Go to Radzen Blazor Studio

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

OrderDetail.Custom.cs

public partial class OrderDetail
{
  // Additional property
  [NotMapped]
  public decimal? ExtendedPrice
  {
    get;
    set;
  }
}

OrderDetail.Custom.vb

Public Partial Class OrderDetail
    <NotMapped>
    Public Property ExtendedPrice As Decimal?
End Class

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

OrderDetailsController.Custom.cs

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)
                .FirstOrDefault();

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

OrderDetailsController.Custom.vb

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
        Next
    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

NorthwindContext.Custom.cs

public partial class NorthwindContext
{
    partial void OnModelBuilding(ModelBuilder builder)
    {
        builder.Entity<DataContextAdditionalConfiguration.Models.Northwind.OrderDetailsExtended>().HasKey(table => new
        {
            table.OrderID,
            table.ProductID
        });
    }
}

NorthwindContext.Custom.vb

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);
    }
}

Startup.Custom.vb

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