Last active
April 6, 2020 07:32
-
-
Save seankearon/125657b5b3f1f13f1b6857574e2bde9e to your computer and use it in GitHub Desktop.
Using EF Core with CosmosDB in F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open Microsoft.EntityFrameworkCore | |
type [<CLIMutable>] LegacyForm = { | |
id: string // The form's UID. | |
Client: string | |
CompanyId: int | |
Number: string | |
InstallationAddress: string | |
Status: string | |
Type: string | |
Uid: string | |
ModelVersion: string | |
LastUpdatedUtc: DateTime | |
} | |
type LegacyFormsContext = | |
inherit DbContext | |
(* Based on Eelco Mulder's repo here: https://github.com/EelcoMulder/EFCoreWithFSharp *) | |
new() = { inherit DbContext() } | |
new(options: DbContextOptions<LegacyFormsContext>) = { inherit DbContext(options) } | |
override __.OnConfiguring (optionsBuilder: DbContextOptionsBuilder) = | |
optionsBuilder.UseCosmos( | |
"https://localhost:8081", | |
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", | |
"MyDB") |> ignore | |
override __.OnModelCreating modelBuilder = | |
modelBuilder | |
.Entity<LegacyForm>() | |
.HasNoDiscriminator() | |
.ToContainer("LegacyForms") | |
|> ignore | |
[<DefaultValue>] | |
val mutable forms:DbSet<LegacyForm> | |
member x.Forms | |
with get() = x.forms | |
and set v = x.forms <- v | |
module LegacyFormsRepo = | |
let formById (context: LegacyFormsContext) id = | |
query { | |
for form in context.Forms do | |
where (form.id = id) | |
select form | |
exactlyOne | |
} |> (fun x -> if box x = null then None else Some x) | |
let formsForCompany (context: LegacyFormsContext) companyId = | |
query { | |
for form in context.Forms do | |
where (form.CompanyId = companyId) | |
select form | |
skip 4 | |
take 2 | |
} |> Array.ofSeq | |
[<EntryPoint>] | |
let main argv = | |
let context = new LegacyFormsContext() | |
let formId = "ec770405-692d-44cf-a5a7-0c0fd7ca1f3f" | |
let form = LegacyFormsRepo.formById context formId | |
let forms = LegacyFormsRepo.formsForCompany context 123 | |
forms |> printfn "%A" | |
Console.Read() |> ignore | |
0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment