I'm trying to rewrite a C# EF Core example (similar to the one in the docs) into F#. I have a problem with HasIndex. In C#, the call is HasIndex(b => b.Isbn)
, to create an index on the Isbn column:
public class SampleContext : DbContext
{
public DbSet<Book> Books { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Book>().HasIndex(b => b.Isbn);
}
}
Doing the same in F#, HasIndex(fun b -> b.Isbn)
, doesn't compile:
type public SampleContext() =
inherit DbContext()
[<DefaultValue>]
val mutable books: DbSet<Book>
member x.Books
with get() = x.books
and set v = x.books <- v
override __.OnModelCreating(modelBuilder) =
modelBuilder.Entity<Book>().HasIndex(fun b -> b.Isbn) // doesn't compile
|> ignore
Do I need to use some quotation magic to make this work?
(You can of course use a string, HasIndex("Isbn")
, but I'd prefer not to throw compile time checks out the window.)
fun b -> b.Isbn
in <@ @> and convert the resultingExpr
intoExpression
with this msdn.microsoft.com/visualfsharpdocs/conceptual/… – Honza Brestan Mar 31 '18 at 17:47LeafExpressionConverter.QuotationToLambdaExpression <@ Func<_, _>(fun (i: Book) -> i.Isbn :> obj) @>
did the trick. If you write that as an answer, I'll mark it as accepted. – John Reynolds Apr 1 '18 at 0:37