LINQ 函数和 DataContext 处置、延迟执行

发布于 2025-01-02 14:44:37 字数 740 浏览 1 评论 0原文

所以我在这里需要一些建议和见解。预先感谢您的想法。

我开发了从 LINQ 实体返回单个记录的静态函数。像这样:

FooRecord GetRecord(Guid id)
{
   using(var dc = new FooDataContext())
       return dc.FooRecords.Where(a => a.Id == id).First();
}

这会引发异常,因为 DataContext 已经被释放,这会产生延迟执行的问题。这有效:

FooRecord GetRecord(Guid id)
{
       var dc = new FooDataContext();
       return dc.FooRecords.Where(a => a.Id == id).First();
}

我很担心。 DataContext 的处理速度有多快?显然,如果我立即获取记录,这不会造成问题。但是,假设我需要通过关联获取记录:

var record = Data.FooRecord.GetRecord(id);
//Do a bunch of stuff...
//Now we grab the related record from another entity
var barRecord = record.BarRecord

此时 DataContext 是否存在消失的风险?有什么建议吗?

So I need some advice and insight here. Thanks in advance for your thoughts.

I have developed static functions that return a single record from a LINQ entity. Like so:

FooRecord GetRecord(Guid id)
{
   using(var dc = new FooDataContext())
       return dc.FooRecords.Where(a => a.Id == id).First();
}

This throws an exception because the DataContext is already disposed, which creates problems with deferred execution. This works:

FooRecord GetRecord(Guid id)
{
       var dc = new FooDataContext();
       return dc.FooRecords.Where(a => a.Id == id).First();
}

I am worried. How quickly will the DataContext be disposed? Obviously if I grab the record immediately this won't cause an issue. However, say I need to grab a record through association:

var record = Data.FooRecord.GetRecord(id);
//Do a bunch of stuff...
//Now we grab the related record from another entity
var barRecord = record.BarRecord

Is there a risk the DataContext be gone by this point? Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半山落雨半山空 2025-01-09 14:44:37

由于此处讨论的原因,您基本上不需要 Dispose() 您的 DataContext:

我应该什么时候处理数据上下文

http://csharpindepth.com/ViewNote.aspx?NoteID=89

在类型上实现 IDisposable 的主要原因是处置任何非托管资源。 DataContext 分配的唯一非托管资源是底层数据库连接,但 DataContext 已经负责根据需要打开和关闭连接。

您要避免的主要事情是返回 IEnumerable 集合,然后从不枚举它,因为这将导致连接无限期地保持打开状态。但是,由于您只返回一个对象,因此不必担心这一点。

另请注意,如果访问返回对象上的任何关系属性,可能会导致连接暂时重新打开,以便可以延迟加载该属性。您可以通过将 DataLoadOptions.LoadWith() 与 DataContext 一起使用来预先加载您想要访问的任何属性来避免这种情况。请参阅 http://msdn.microsoft.com/en -us/library/system.data.linq.dataloadoptions.aspx

至于问题的最后一部分,如果返回的实体包含可以延迟加载的属性,那么它们将包含内部引用来支持 DataContext将要将其保留在记忆中。一旦不再有对这些实体的引用,那么 DataContext 当然会像任何其他对象一样被垃圾收集。

You basically do not need to Dispose() your DataContext for the reasons discussed here:

When should I dispose of a data context

http://csharpindepth.com/ViewNote.aspx?NoteID=89

The main reason for implementing IDisposable on a type is to dispose of any unmanaged resources. The only unmanaged resource allocated by the DataContext is the underlying database connection, but the DataContext already takes care of opening and closing the connection as needed.

The main thing you want to avoid is returning an IEnumerable collection and then never enumerating it, as this will cause the connection to remain open indefinitely. However, since you are only returning a single object, you shouldn't have to worry about this.

Also note that if access any relationship property on the returned object it may cause the connection to be momentarily reopened so that the property can be lazy loaded. You can avoid this by using DataLoadOptions.LoadWith() with your DataContext to eager-load any properties you intend to access. See http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.aspx

As to the last part of the question, if the returned entities contain properties that can be lazy loaded, then they will contain internal references to back the DataContext that will keep it in memory. Once you have no more references to these entities, then the DataContext will of course be garbage-collected just like any other object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文