Search code examples
c#datagridblazorsyncfusion

Composite key in a Syncfusion Blazor Datagrid


I need advice on implementing a composite key on a Syncfusion for a Blazor Datagrid. It does not allow composite keys and the ticket to add it as a feature has sat unmoved for a few years now.

This is the TimeUnit class that uses a composite key of ID and Valid From Date:

    public class TimeUnit
    {
        public Int16 timeUnitID { get; set; }
        public DateTime validFrom { get; set; }
        public DateTime? validTo { get; set; }
        public string description { get; set; }
        public Nullable<Int16> days { get; set; }
        public Common.SpecialProcessing specialProcessing { get; set; }
        public DateTime dateLastActivity { get; set; }
    }

And this an extract from dbContext where the key is defined

            builder.Entity<TimeUnit>().HasKey(table => new
            {
                table.timeUnitID,
                table.validFrom
            });

That format has stood the test of time through Windows Froms, WPF, and Angular using PrimeNG. But not Syncfusion for a Blazor Datagrid.

I'm thinking of adding something like

[Not Mapped]
public Int16 timeUnitID { get; set; }

to the class and filling it with sequential integers after the fetch. I could offer it as a sacrifical key to the Syncfusion Blazor Datagrid.

The attributes of a TimeUnit ID can be different at different points in time - this applies to many of my definitions, so this key format needs to persist.

I am asking for suggestions on a workaround before I pursue the one I have outlined here.


Solution

  • Currently we do not have support for composite keys in Syncfusion Blazor DataGrid component. But from your update we suspect that you want to have two columns (timeUnitID and validFrom) to be disabled from editing or adding since they were autogenerated from your database. If yes, suggest you to achieve your requirement by defining the timeUnitID property as Key (IsPrimaryKey) in Grid component and IsIdentity property to both the properties (timeUnitID and validFrom). Defining the grid column as IsIdentity will make these properties disabled while editing and adding records in Grid and values can be autogenerated while saving into databased.

    Refer to our UG and API documentation for your reference.

    https://blazor.syncfusion.com/documentation/datagrid/editing - Notes sections. https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridColumn.html#Syncfusion_Blazor_Grids_GridColumn_IsIdentity https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridColumn.html#Syncfusion_Blazor_Grids_GridColumn_IsPrimaryKey

    Please get back to us if you have further queries.