如何为Entity Framework创建一个虚拟记录字段懒惰加载 [英] How to create a virtual record field for Entity Framework lazy loading

查看:115
本文介绍了如何为Entity Framework创建一个虚拟记录字段懒惰加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定义一些我在Entity Framework 6中使用的模型。我想使用F#记录。如果我将 [< CLIMutable>] 高于记录定义,并使用 ICollection<'a> 收藏。 EF工作正常

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.

我可以存储和检索记录。然而对于EF做懒惰加载,我需要使包含该集合的记录字段为虚拟。我可以通过使其成为抽象的并提供默认值来使虚拟的属性成员。记录不是类。是否可以创建一个记录字段?

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

推荐答案

据我所知,您无法定义虚拟成员一个记录 - 这是有道理的,因为你也不能从记录中继承,所以你为什么需要虚拟成员。

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.

但是我可以看到为什么实体将需要这个框架。也许这样的东西就像 CliVirtual 属性,类似于 CliMutable (特别是如果这种情况发生)。请随时在 F#用户声明页面上提出建议!

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

或者也可以看看F#的其他SQL类型提供者是否能让您更好地做同样的事情!

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

这篇关于如何为Entity Framework创建一个虚拟记录字段懒惰加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆