实体框架:重新查找最近添加到上下文的对象 [英] Entity Framework: Re-finding objects recently added to context

查看:17
本文介绍了实体框架:重新查找最近添加到上下文的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架,但在重新查找"我刚刚创建的对象时遇到了问题...基本上是这样的:

I am using the entity framework and I'm having a problem with "re-finding" objects I just created... basically it goes like this:

string theId = "someId";

private void Test()
{
  using(MyEntities entities = new MyEntities())
  {
    EntityObject o = new EntityObject();
    o.Id = theId;
    entities.AddToEntityObject(o);
    CallSomeOtherMethod(entities);
  }
}

void CallSomeOtherMethod(MyEntities ents)
{
  EntityObject search = ents.EntityObject.FirstOrDefault(o => o.Id == theId);
  if(search == null) 
  {
    Console.WriteLine("wha happened???");
  }
}

(顺便说一句,不能保证代码有效 - 这一切都是我的想法)

(no guarantee the code works btw - it's all from my head)

为什么查询没有找到"刚刚创建的 EntityObject?

Why doesn't the query "find" the EntityObject that was just created?

如果我在 AddToEntityObject 之后调用 SaveChanges() 它可以工作(这并不让我感到惊讶)但为什么它不能正确地从缓存中提取?

If I call SaveChanges() after the AddToEntityObject it works (which doesn't surprise me) but why doesn't it pull from the cache properly?

我仍然对这些东西持怀疑态度,所以我希望有一些我只是忽略的非常简单的事情......

I'm still green on this stuff so I'm hoping that there's some really easy thing that I'm just overlooking...

谢谢

推荐答案

出现这种情况是因为 ents.EntityObject.WhatEver 总是查询数据源.这是一个设计决定.他们这样做是因为否则他们将不得不对数据源、本地缓存执行查询,然后合并结果.正如其中一位开发人员在博客中指出的那样(不记得具体在哪里),他们无法始终如一地处理这个问题.

This happens because ents.EntityObject.WhatEver always queries the datasource. This is a design decision. They do it this way, because else they would have to execute the query against the datasource, against the local cache and then merge the results. As one of the developers pointed out in a blog (cannot remember where exactly) they were unable to handle this consistently.

正如您想象的那样,您必须正确处理许多角落和边缘情况.您可以找到您在本地创建的、由其他人在数据库中创建的 ID.这将迫使您准备好处理(几乎)每个查询的冲突.也许他们可以制定查询本地缓存的方法和查询数据源的方法,但这也不聪明.

As you can imagine there are a lot of corner an edge cases you have to handle properly. You could just find a id you created locally, created by someone else in the database. This would force you to be prepared to handle conflicts on (almost) every query. Maybe they could have made methods to query the local cache and methods to query the datasource, but that is not to smart, too.

您可以查看实体框架的透明延迟加载.这取代了普通的代码生成器,您将获得在访问时自动填充其相关实体集合和实体引用的实体.这避免了所有

You may have a look at Transparent Lazy Loading for Entity Framework. This replaces the normal code generator and you get entities that populate their related entity collections and entity references automatically on access. This avoids all the

if (!Entity.ReleatedEntities.IsLoaded)
{
   Entity.RelatedEntities.Load();
}

代码片段.您可以查询集合,因为它们总是被隐式加载.但是这个解决方案也不是完美的.有一些问题.例如,如果您创建一个新实体并访问相关实体的集合,您将收到异常,因为代码无法从数据库中检索相关实体.还有一个关于数据绑定的问题,可能还有更多我不知道的问题.

code fragments. And you can query the collections because they are always implicitly loaded. But this solution is not perfect, too. There are some issues. For example, if you create a new entity and access a collection of related entities, you will get an exception because the code is unable to retrieve the related entities from the database. There is also an issue concerning data binding and may be some more I am not aware of.

好消息是您可以获得源代码并能够自己解决问题,如果我有时间,我将检查第一个问题.但我很确定它不会那么容易修复,因为我预计如果实体刚刚创建不是预期的行为,那么某些情况就不会命中数据库.

The good thing is that you get the source code and are able to fix the issues yourself and I am going to examine the first issue if I find some time. But I am quite sure that it will not be that easy to fix, because I expect some case were just not hitting the database if the entity has just been created is not the expected behavior.

这篇关于实体框架:重新查找最近添加到上下文的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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