You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The F# Discriminated Union (DU) is a sum type where a type can contain a value of (e.g.) type A or B, but not both, for instance
typeUserType=| User
| Admin
The constituent types can be empty like above, or can have associated values, including plain types, complex types like records, or even other union types
typePerson={Id:int; First:string; Last:string}// define a record typetypeIntOrBool= I ofint| B ofbooltypeMixedType=| Tup ofint*int// a tuple| P ofPerson// use the record type defined above| L ofintlist// a list of ints| U ofIntOrBool// use the union type defined above
EFCore.FSharp needs to be able to handle these types and
Generate valid migrations that can encode such a union
Existing tables with discriminator types should be mapped as a discriminated union.
Migrations
Each DU should be mapped to a table with a discriminator column and each associated type modelled aa linked entity in its own right. This approach will result in separate tables in the example above that model each field type
CREATETABLEPersonId
(
Id INT,
First NVARCHAR,
Last NVARCHAR
)
CREATETABLEIntOrBool
(
Id INT,
Discriminator
Int1 INT,
Bool1 BIT
)
CREATETABLEIntOrBool_Int
(
Id INT,
Int1 INT
)
CREATETABLEIntOrBool_Bool
(
Id INT,
Bool1 BIT
)
CREATETABLEMixedType
(
Id INT,
Discriminator NVARCHAR
)
CREATETABLEMixedType_Tup
(
Id INTPRIMARY KEY,
MixedTypeId INTFOREIGN KEY, -- UNIQUE
Int1 INT,
Int2 INT
)
CREATETABLEMixedType_P
(
Id INTPRIMARY KEY,
MixedTypeId INTFOREIGN KEY,
PersonId INTFOREIGN KEY
)
CREATETABLEMixedType_L
(
Id INTPRIMARY KEY,
MixedTypeId INTFOREIGN KEY, -- Duplicates allowed as it models a list
Int1 INT
)
CREATETABLEMixedType_U
(
Id INTPRIMARY KEY,
MixedTypeId INTFOREIGN KEY,
IntOrBool INTFOREIGN KEY
)
This results in a lot of tables being generated but is I believe the most flexible system allowing for further expansion of the domain in future, for instance, if a new property was added to Person, or a new entry included in MixedType.Tup
It also allows the union type to be extended easily, just adding a new table instead of adding additional columns to existing tables
Scaffolding
We will need to be able to support scaffolding code from existing tables that would also include types such multiple types per table
The F# Discriminated Union (DU) is a sum type where a type can contain a value of (e.g.) type
A
orB
, but not both, for instanceThe constituent types can be empty like above, or can have associated values, including plain types, complex types like records, or even other union types
EFCore.FSharp needs to be able to handle these types and
Migrations
Each DU should be mapped to a table with a discriminator column and each associated type modelled aa linked entity in its own right. This approach will result in separate tables in the example above that model each field type
This results in a lot of tables being generated but is I believe the most flexible system allowing for further expansion of the domain in future, for instance, if a new property was added to
Person
, or a new entry included inMixedType.Tup
It also allows the union type to be extended easily, just adding a new table instead of adding additional columns to existing tables
Scaffolding
We will need to be able to support scaffolding code from existing tables that would also include types such multiple types per table
Example from #21:
Where a many-types-per-table relationship from a Table
Person
with Discrimator values ofEmployee
andManager
would be modelled in C# aswe should model it as
A potential issue here will be mapping columns to multiple types
The text was updated successfully, but these errors were encountered: