对象集< T> .AddObject()与EntityCollection< T>。新增() [英] ObjectSet<T>.AddObject() vs. EntityCollection<T>.Add()

查看:162
本文介绍了对象集< T> .AddObject()与EntityCollection< T>。新增()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有两个EntitySets,团队和运动员。

Let's say I have two EntitySets, "Teams" and "Players".

我加入新车队的系统,为便于讨论,让我们说,我是从一个文件中添加了一千团队(其中包含重复)。

I am adding new teams to the system, for sake of argument, let's say I'm adding a thousand teams from a file (which contains duplicates).

该系统包含100个团队开始。而我的目标是避免重复,而无需调用的SaveChanges()为每个团队的补充。

The system contains 100 teams to start. And my goal is to avoid duplicates without calling SaveChanges() for every team added.

这个过程是查询该newteam不存在,然后添加新的团队,如果不存在的。

The process is to query that the newteam doesn't already exist, Then add the new team if not exist.

    var team = (from t in context.Teams
    where t.Name == newTeam.Name
    select t).FirstOrDefault();

if (team==null)
--Add New Team

如果我添加使用 context.Teams.AddObject(newTeam); context.AddObject(团队,newTeam);

在team.Count()将保持100%,如果你再次运行查询,VAR队会是空的。

The team.Count() will remain 100 and if you ran the query again, var team would be null.

不过,如果我改用 player.Teams.Add(newTeam); 添加了球队,一切完美的作品,但我也有一个玩家添加NewTeam至该玩家将所有的新车队中的一员。

However, If I instead use player.Teams.Add(newTeam); to add the team, everything works perfectly, except I have to have a player to add the NewTeam to and that player will be a member of all new teams.

调用后 player.Teams.Add(newTeam); 该查询将返回新的团队。

After calling player.Teams.Add(newTeam); The query will return the new team.

 var team = (from t in player.teams
where t.Name = newTeam.Name
select t).FirstOrDefault();

player.Teams.Count()将加一。

在第一个例子context.Teams是对象集< T> 而在player.Teams.Add团队是一个 EntityCollection< T&GT ; 。我认为这会表现得一样的,但他们没有。

In the first example context.Teams is an ObjectSet<T>, whereas, in player.Teams.Add Teams is an EntityCollection<T>. I would think these would behave the same, but they do not.

有没有办法让对象集&LT; T&GT; 行为类似于 EntityCollection&LT; T&GT; ?我想一次,只有所有的球队已被添加在文件后一次调用的SaveChanges。

Is there a way to make the ObjectSet<T> behave like the EntityCollection<T>? I want to call SaveChanges once and only once after all teams have been added from the file.

还是有更好的办法,我没有看到?

Or is there a better way that I am not seeing?

谢谢!

推荐答案

没有就没有办法使他们的行为是一样的。 对象集重presents数据库查询,一旦你使用它,你总是在做查询到新的团队是不是present但数据库。 EntityCollection 是加载实体地方集合,如果你使用它,你正在做的查询到你的应用程序的内存。

No there is no way to make them behave the same. ObjectSet represents database query and once you use it you are always doing query to the database where your new team is not present yet. EntityCollection is local collection of loaded entities and if you use it you are doing query to your application memory.

通常使用 EntityCollection 是完全一样保持独立的名单,其中,团队及GT;

Generally using EntityCollection is exactly same as maintaining separate List<Team>:

List<Team> teams = context.Teams.ToList();

var team = teams.FirstOrDefault(t => t.Name == newTeam.Name);
if (team == null) 
{
    context.Teams.AddObject(newTeam);
    teams.Add(newTeam);
}

context.SaveChanges();

您也可以使用词典&LT;字符串,团队及GT; 并获得搜索列表中为每个团队可能是更好的性能,而不是

You can also use Dictionary<string, Team> and get probably better performance instead of searching the list for each team.

这篇关于对象集&LT; T&GT; .AddObject()与EntityCollection&LT; T&GT;。新增()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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