Black Friday Deals

Up to $400 OFF

Check Now →

Extend model with additional property (Blazor)

This guide demonstrates how to extend model with additional property.

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

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

server\Services\NorthwindService.Custom.cs

using SampleBlazor.Models.Northwind;
using System.Linq;

namespace SampleBlazor
{
    public partial class NorthwindService
    {
        partial void OnOrderDetailsRead(ref IQueryable<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;
                }
            }
        }
    }
}

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

4. Run the application