Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a virtual record field for Entity Framework lazy loading

I'm trying to define some models that I use in Entity Framework 6. I would like to use F# records. If I put [<CLIMutable>] above the record definition and use ICollection<'a> for, well, the collections. EF works fine.

I can store and retrieve the records. However for EF to do lazy loading I need to make the record field containing the collection virtual. I can make a property member virtual by making it abstract and providing a default. Records are not classes. Is it possible to make a record field virtual?

If I try to augment the record with an abstract member I get this error this declaration element is not permitted in an augmentation

like image 506
Remko Avatar asked Nov 06 '14 09:11

Remko


1 Answers

As far as I know, you cannot define a virtual member in a record - this makes sense, because you also cannot inherit from a record, so why would you need virtual members.

But I can see why this would be needed for Entity Framework. Perhaps it would make sense to have something like CliVirtual attribute, akin to CliMutable (especially if this happens often). Feel free to suggest this at the F# uservoice page!

So, I guess your best option is to use ordinary class with virtual auto-implemented properties (which looks significantly uglier):

type A() =
  abstract member Id: int with get, set
  default val Id = 123 with get, set

Or perhaps see if some of the other SQL type providers for F# let you do the same thing more nicely!

like image 105
Tomas Petricek Avatar answered Nov 15 '22 07:11

Tomas Petricek