ADO.NET实体框架IsLoaded和加载 [英] ADO.NET Entity Framework IsLoaded and Load
问题描述
VB:
$ b我发现自己重复了一遍这样的代码,一直使用ADO.NET Entity Framework。 $ b
'加载的东西,如果还没有加载。 '
如果不是Something.Thing.IsLoaded然后
Something.Thing.Load()
如果
C#:
//加载的东西,如果还没有加载。
if(!Something.Thing.IsLoaded)
{
Something.Thing.Load();
}
这是正常吗?我应该使用 IsLoaded 和加载经常?或者我不正确使用这个?
VS2010 beta 在这方面是非常友好的。也就是说,如果您没有延迟加载版本,该扩展方法可能会帮助您:
public static T在这里,T:class,IEntityWithRelationships
{
if(!eRef.IsLoaded)
eRef.Load();
return eRef.Value;
}
然后,假设您有具有联系人的User对象,您可以执行以下操作: / p>
联系c = User.ContactReference.EnsureLoaded();
它仍然很可爱,但我发现最好一遍又一遍地编写IsLoaded if语句。 / p>
I find myself repeating bits of code like this over and over while using ADO.NET Entity Framework.
VB:
' Load the thing if not already loaded. '
If Not Something.Thing.IsLoaded Then
Something.Thing.Load()
End If
C#:
// Load the thing if not already loaded.
if (!Something.Thing.IsLoaded)
{
Something.Thing.Load();
}
Is this normal? Should I be using IsLoaded and Load so often? Or am I not using this correctly?
The version of the EntityFramework in the VS2010 beta is much, much friendlier in this regard. That said, if you're stuck with the version without lazy-loading, this extension method may help you:
public static T EnsureLoaded<T>(this EntityReference<T> eRef) where T: class, IEntityWithRelationships
{
if (!eRef.IsLoaded)
eRef.Load();
return eRef.Value;
}
Then, assuming you have User objects that have Contacts, you could do:
Contact c = User.ContactReference.EnsureLoaded();
It's still pretty sucky, but I find it preferable to writing that IsLoaded if statement over and over.
这篇关于ADO.NET实体框架IsLoaded和加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!