C
C#10mo ago
engineertdog

Best way to model different options in EF Core

I have a class that's used to define the integration parameters for a particular process. Now, I have integration options A,B,C, etc each of which correspond to different tables with different IDs I need to populate on my class. ex
c#
public class myclass {
public string MyParam1 { get; set; }
public string MyParam2 { get; set; }
public string MyParam3 { get; set; }

// Integration A
public Guid PackageId { get; set; }
public Guid LocationId { get; set; }
public Guid ScreenElementId { get; set; }
// Integration B
public Guid PackageId { get; set; }
public Guid LocationId { get; set; }
public Guid ScreenElementId { get; set; }
// etc
}
c#
public class myclass {
public string MyParam1 { get; set; }
public string MyParam2 { get; set; }
public string MyParam3 { get; set; }

// Integration A
public Guid PackageId { get; set; }
public Guid LocationId { get; set; }
public Guid ScreenElementId { get; set; }
// Integration B
public Guid PackageId { get; set; }
public Guid LocationId { get; set; }
public Guid ScreenElementId { get; set; }
// etc
}
Now, only 1 integration is supported for each instance of myclass. I could in theory define each available integration's FK references in this table, but there must be a better way to do this?
c#
public class myclass {
public string MyParam1 { get; set; }
public string MyParam2 { get; set; }
public string MyParam3 { get; set; }

// Integration
public IntegrationType Integration { get; set; } // A,B,C,etc
public Guid IntegrationId { get; set; } // But wouldn't this just then be a table with all references, like in the previous example?
}
c#
public class myclass {
public string MyParam1 { get; set; }
public string MyParam2 { get; set; }
public string MyParam3 { get; set; }

// Integration
public IntegrationType Integration { get; set; } // A,B,C,etc
public Guid IntegrationId { get; set; } // But wouldn't this just then be a table with all references, like in the previous example?
}
3 Replies
Omnissiah
Omnissiah10mo ago
either you use you discriminator and a flat type witih all the FKs, or you make myClass three types each with its own FK probably the two simplest roads
engineertdog
engineertdogOP10mo ago
I was thinking of keeping the bottom half, but modified a little.
c#
public class myclass {
public string MyParam1 { get; set; }
public string MyParam2 { get; set; }
public string MyParam3 { get; set; }

// Integration
public IntegrationType Integration { get; set; } // A,B,C,etc
public Guid? IntegrationAId { get; set; }
public Guid? IntegrationBId { get; set; }
public Guid? IntegrationCId { get; set; }
}
c#
public class myclass {
public string MyParam1 { get; set; }
public string MyParam2 { get; set; }
public string MyParam3 { get; set; }

// Integration
public IntegrationType Integration { get; set; } // A,B,C,etc
public Guid? IntegrationAId { get; set; }
public Guid? IntegrationBId { get; set; }
public Guid? IntegrationCId { get; set; }
}
I don't expect to have that many integrations, but with the other params required, it doesn't make sense to have a version of the class for each type. Makes more sense to use a type to determine the integration and reference the proper FK
Omnissiah
Omnissiah10mo ago
wait, do all integrations have the same fields?
Want results from more Discord servers?
Add your server