0

I have a data structure for an automation device. It has multiple outputs, those are organized into a layout.

    public class AutomationLayout
{
    public Pin D0 { get; set; }
    public Pin D1 { get; set; }
    public Pin D2 { get; set; }
    public Pin D3 { get; set; }
    public Pin D4 { get; set; }
    public Pin D5 { get; set; }
    public Pin D6 { get; set; }
    public Pin D7 { get; set; }
    public Pin D8 { get; set; }
    public Pin D9 { get; set; }
}

Each output has multiple dependencies.

    public class Pin
{
    public string Name { get; set; }
    public string DigitalSensor { get; set; }
    public Schedule Schedule { get; set; }
    public ClimateControl ClimateControl { get; set; }
    public TempRange TempRange { get; set; }
    public VoltRange VoltRange { get; set; }
}

And those classes are also full of properties.

Database diagram

I have used Database first approach with EF Core.

I am mapping the created models to DTOs and use these in my controller.

    public static class DeviceDtoMapper
{
    public static DeviceGetDto MapDeviceToGetDto(Device device)
    {
        return new DeviceGetDto()
        {
            Id = device.Id,
            Name = device.Name,
            CreatedAt = device.CreatedAt
        };
    }

    public static Device MapPostDtoToDevice(DevicePostDto deviceDto)
    {
        return new Device()
        {
            Name = deviceDto.Name,
            CreatedAt = DateTimeOffset.Now.ToString()
        };
    }

    public static Device MapPutDtoToDevice(DevicePutDto deviceDto)
    {
        return new Device()
        {
            Name = deviceDto.Name
        };
    }
}

I am trying to write a controller to GET the complete automation layout.

The problems is it would require more than a 1000 Include() and ThenInclude() lines, which does not seem to be very efficient.

If possible I would avoid including all the navigation properties.

How can I deal with this data structure?

Thanks in advance.

nasarhon
  • 21
  • 4
  • Please provide the data structure and the mappaing ( if you did custom ) – miechooy Apr 08 '22 at 00:02
  • 1
    Does this answer your question? [Entity Framework Core 2.0.1 Eager Loading on all nested related entities](https://stackoverflow.com/questions/49593482/entity-framework-core-2-0-1-eager-loading-on-all-nested-related-entities) – Yong Shun Apr 08 '22 at 00:20
  • 1
    That's a terrible DB design. `AutomationLayout` should define a normal 1-many navigation `public virtual ICollectoin Pins { get; set; }`. And `Pin` should have a `0-9` index column. – Jeremy Lakeman Apr 08 '22 at 01:25
  • Thank you for the suggestion @JeremyLakeman , this was my first DB, I will work through the project with your idea, to see if it works for our usecase, but I think it will. After that I will try to implement the answer suggested above, if its still needed. – nasarhon Apr 08 '22 at 06:18
  • You can drill down through relationships to include multiple levels of related data using the ThenInclude method. But if you need to include 1000 times then EF Core don't provide anything for it to avoid it. – Deepak-MSFT Apr 08 '22 at 08:03

1 Answers1

0

The solution was the changing of the DB design according to @JeremyLakeman suggestion.

With indexed object collections, I only had to map the structure once with the correct series of 'Includes' and 'ThenIncludes'.

nasarhon
  • 21
  • 4