实体框架.load(MergeOption)有什么作用? [英] What does entity framework .load(MergeOption) do?

查看:83
本文介绍了实体框架.load(MergeOption)有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试升级使用Entity Framework 4的大型应用程序以使用Entity Framework 5.我发现了这样的功能:

 公共FooModel(FooEntity foo){_foo = foo;_foo.bars.Load(System.Data.Objects.MergeOption.OverwriteChanges);} 

其中 foo bar 是生成的实体,其中 bar 具有 foo 的外键.

似乎EF5不再具有 .Load(MergeOption)功能,而我之前从未见过.有谁知道它的作用,以及它的等效作用是什么?

https://stackoverflow.com/a/13178313/784908 建议 Load 是DbContext的一部分-但我的实体容器是从DbContext继承的,但仍然不可用


我的最佳猜测是,它用于外键的急切加载(我需要这样做,上下文在请求中创建和处置了很多次,并且没有保证,当FooModel时它将存在/附加)使用)

实体框架-渴望加载相关实体表明我应该使用 .Include(),但是该功能似乎在实际实体上不可用(我认为该术语是物化查询"吗?)

感谢阅读

解决方案

.Load()将数据库中的 IQueryable 查询加载到内存中-实际上, Local 属性.

您可以将此方法与任何IQueryable集合一起使用,而不仅限于DbContext.示例如下:

  var q = db.Products.Include("Category").ToList();q.Load();//->你不能!//------------db.Products.Include("Category").Load();//没关系!//这不会查询数据库,只会查询内存中的数据.var p = db.Products.Local.Single(id);//------------var q = db.Products.Include("Category").ToList();q.AsQueryable().Load();//->没关系!//这也不会查询数据库,只会查询内存中的数据.var p = db.Products.Local.Single(id); 

主要使用load函数有两个原因:

1)从Db检索部分数据到内存并使用它们:

  db.Products.Include("Category").Where(p =&>; p.CatId == 10).Load(); 

2)能够使用L2E不支持的Linq-to-Objects方法(例如.ToString()等)-因为DbContext实体的Local属性是 ObservableCollection< T> IEnumerable 的code>,就像L2O对象一样:

  db.Products.Include("Category").Where(p =&>; p.CatId == 10).Load();字符串subName = db.Products.Local.Find(id).SubString(0,4); 

I'm trying to upgrade a large application which uses Entity Framework 4 to use Entity Framework 5. I've discovered a function like this:

public FooModel(FooEntity foo)
{
    _foo = foo;
    _foo.bars.Load(System.Data.Objects.MergeOption.OverwriteChanges);
}

Where foo and bar are generated entities, with bar having a foreign key to foo.

It seems EF5 no longer has the .Load(MergeOption) function, and I've never seen it before. Does anyone know what it does, and what its equivalent is?

https://stackoverflow.com/a/13178313/784908 suggests that Load is part of DbContext - but my entity container inherits from DbContext, and still isn't available


My best guess is that it is used for Eager loading of the foreign keys (which I need to do, the context is created and disposed of many times in a request, and there is no guarentee it will exist/attached when FooModel is used)

Entity Framework - eager loading of related entities shows I should be using .Include(), but that function doesn't seem to be available on an actual entity (I think the term is 'materialized query'?)

Thanks for reading

解决方案

.Load() loads an IQueryable query from database into the memory - in fact, Local property of the relevant entity of your DbContext.

You can use this method with any IQueryable collection, not just the DbContext. Examples are as the following:

var q = db.Products.Include("Category").ToList();
q.Load(); // -> you can't!

// ------------

db.Products.Include("Category").Load(); // It's OK!

// This will NOT query the database, just looks in-memory data.
var p = db.Products.Local.Single(id); 

// ------------

var q = db.Products.Include("Category").ToList();
q.AsQueryable().Load(); // -> It's OK!

// This also will NOT query the database, just looks in-memory data.
var p = db.Products.Local.Single(id); 

The load function is mainly used for 2 reasons:

1) Retrieving some parts of data from Db to memory and work with them:

db.Products.Include("Category").Where(p => p.CatId == 10).Load();

2) To be able to use Linq-to-Objects methods which L2E doesn't supports(like .ToString(), etc.) - as the Local property of DbContext entities are ObservableCollection<T> which implements IEnumerable, just as L2O objects:

db.Products.Include("Category").Where(p => p.CatId == 10).Load();
string subName = db.Products.Local.Find(id).SubString(0, 4);

这篇关于实体框架.load(MergeOption)有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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