Open EspenBrun opened 1 year ago
This works
let duValueConverter<'a> =
ValueConverter<'a, string>((fun v -> v.ToString()), (fun v -> fromStringOrFail<'a>(v)))
and
b.Property<BlogType>("BlogType")
.HasConversion (duValueConverter<BlogType>)
|> ignore
Would still be nice to just have a registerEmptyUnionCases()
that handled this for every empty DU
The fromStringOrFail
utility for anyone intersted:
// utility
let inline fromStringOrFail<'a> (s: string) =
let duType = typeof<'a>
match
FSharpType.GetUnionCases duType
|> Array.filter (fun case -> case.Name = s.Replace(" ", ""))
with
| [| case |] -> FSharpValue.MakeUnion(case, [||]) :?> 'a
| _ -> failwith $"Could not parse {s} to {duType.Name}"
Hello,
Thanks for this awesome package!
Is your feature request related to a problem? Please describe. I would like to use an empty discriminated union to hold in my records
Describe the solution you'd like Something like
registerEmptyUnionCases()
, so I can just use mye EquineBeast DU as normal in by fsharp code.If a define an Enum, it will be stored as 0, 1, 2 in the database.
Describe alternatives you've considered If I could define a value converter on the ModelSnapshot that would also solve my problem (although registering in _.OnConfigure once would be much preferred). I have tried something like this with no luck (does not compile)