'DataSet Parent-Child Nested Relation in Blazor

Is DataSet Parent-Child nested relation available in blazor? if yes, how to apply or use it?

Thanks.



Solution 1:[1]

what i'm trying to do is building something like nested repeater but in blazor

At its simplest you'd perhaps have a DB table pair, one Company and many Employee

class Company{
  int Id {get;set;}
  string Name {get;set;}
  ICollection<Employee> Employees {get;set;}
};

class Employee{
  int Id {get;set;}
  string Name {get;set;}
  int CompanyId {get;set;}
  Company Company {get;set;}
};

You'd get EFC to download them for you and fix them up into company manager and related employee details

    
  override async Task OnInitializedAsync(){

    var context = new YourdbContext();

    _companies = context.Companies.Include(c => c.Employees).ToListAsync();
    

  }

And this would be on a razor page like

@page "someurl/here"

@foreach(var c in _companies){
    <h3>@c.Name</h3>
    @foreach(var e in c.Employees){
        <div>@e.Name</div>
    }
}
@code{

    List<Company> _companies;

    //oninitialized here 
}

This isn't intended to be a production grade example. In the fullness of time I'm sure you'd fill it out to more, maybe map the db entities to view models and enumerate those, make components per employee and company etc ..

..but as a basic dump we use something like Entity Framework Core, which understands the relationship between our objects and the relationship between our tables, and will download all the companies and all the employees and figure out which goes with which.

What used to be a data relation is now an object parent with a list of object children inside. If you modify any object EFC detects it, and will persist the change (like DataRow row state and the adapter). If you clear the children collection or remove the parent EFC will treat related data in the manner it's been coded to with the cascading delete behavior, like a data relation used to

Further reading

Read https://docs.microsoft.com/en-us/aspnet/core/blazor/blazor-server-ef-core?view=aspnetcore-6.0 if you plan on injecting a context.

If you want to lower the bar for getting EFC up and running I would genuinely consider creating the db first, installing EFCorePowerTools and reverse engineering the db into your code; what is a quick "make two tables, 5 columns, one db diagram and drag a relationship between them, like we did in a dataset" then becomes a fully set up context, with entity classes and properties all done in a few seconds; no typing c# required

Henk's given a link to a good blog, there are loads of component libraries out there with table controls that can do funky "click the row to show the child" (I use MudBlazor and Blazorise)..

..this is just intended to be an absolute bare bones "here's how we behave like a repeater; we put loops that repeat the emitting of markup with context relevant data changing each pass of the loop" to introduce you to notions of how we might code in razor

I have no affiliation with any software mentioned

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1