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

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

问题描述

我正在使用实体框架,我刚刚创建了一个重新找到对象的问题,基本上就是这样:

  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 happen ???);
}
}

(不保证代码工作btw - 我的头)



为什么查询查找刚刚创建的EntityObject?



如果我在AddToEntityObject工作之后调用SaveChanges()(这并不让我感到意外),但是为什么不从缓存中正确拉出?



我还是绿色在这个东西,所以我希望有一些很容易的事情,我只是忽略...



谢谢

解决方案

这是因为ents.EntityObject.WhatEver总是查询数据源。这是一个设计决策。他们这样做,因为否则他们必须针对数据源执行针对本地缓存的查询,然后合并结果。作为开发人员在博客中指出的(不记得在哪里),他们无法一直处理这一点。



你可以想象有很多角落边缘案件你必须正确处理。您可以找到您在本地创建的ID,由数据库中的其他人创建。这将迫使您准备好处理(几乎)每个查询中的冲突。也许他们可以使用查询本地缓存和方法来查询数据源的方法,但这也不是聪明的。



你可以看看实体框架的透明延迟加载。这取代了正常的代码生成器,并且您可以在访问时自动获取填充相关实体集合和实体引用的实体。这避免了所有的

  if(!Entity.ReleatedEntities.IsLoaded)
{
Entity.RelatedEntities。加载();
}

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



好的是,你得到的源代码,并能够修复问题自己,如果我找到一些时间,我将考察第一个问题。但是我确信这不会那么容易修复,因为我希望有些案例只是在数据库中没有达到刚才创建的实体不是预期的行为。


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)

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

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...

Thanks

解决方案

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.

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天全站免登陆