CTP5:向上下文添加实体的最佳实践 [英] CTP5: Best practices for adding entities to the context

查看:65
本文介绍了CTP5:向上下文添加实体的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解EF的内容是如何工作的(特别是使用codefirst ctp5)。  有几种方法可以做同样的事情,我想知道一个是否比另一个更合适。  让我们说我们有两个实体,彼此之间有一对多
的关系。  我们称之为Key和KeyChain。  我想在钥匙串上添加一个密钥,并且可以通过以下方式添加:

I'm trying to understand how the guts of EF works (specifically using codefirst ctp5).  There are a couple ways to do the same thing, and I'm wondering if one is more appropriate than the other.  Lets say we have two entities and they have a one-to-many relationship with each other.  Let's call them Key and KeyChain.  I want to add a key to the keychain, and can do it the following ways:

var key = new Key(){...}

var key = new Key(){...}

keychain.Keys.Add(key);

keychain.Keys.Add(key);

dataContext.SaveChanges();

dataContext.SaveChanges();

var key = new Key(){...}

var key = new Key(){...}

key.KeyChain = keychain;

key.KeyChain = keychain;

dataContext.Keys.Add(key );

dataContext.Keys.Add(key);

dataContext.SaveChanges();

dataContext.SaveChanges();

 

从表面上看,这实现了同样的事情(至少这是我可能天真的理解)。  幕后实际情况有什么不同吗?  如果keychain是KeyChain的新实例,则Keys集合
为null,需要进行实例化。  这样做有什么警告吗?  我更喜欢以一种我没有明确地向datacontext添加内容的方式来做事情,所以我不必传递它。 

On the surface, this accomplishes the same thing (at least this is my potentially naive understanding).  Are there any differences as to what is actually going on behind the scenes?  If keychain is a new instance of KeyChain, then the Keys collection is null and it needs to be instantiated.  Is there any caveats when doing this?  I prefer doing things in a way where i'm not explicitely adding things to the datacontext, so that I don't have to pass it around. 

这是另一种情况我想知道:

Here is another scenario I wonder about:

让我们为我们的模型Lock添加另一个实体,Lock也与Key有一对多的关系。  同样,为上下文添加密钥的两种方法:

Let's add another entity to our model, Lock, who also has a one-to-many relationship with Key.  Again, the two ways of adding a key to the context:

 

var key = new Key(){... }

var key = new Key(){...}

keychain.Keys.Add(key);

keychain.Keys.Add(key);

lock.Keys.Add(key);

lock.Keys.Add(key);

dataContext.SaveChanges();

dataContext.SaveChanges();

var key = new Key(){...}

var key = new Key(){...}

key.KeyChain = keychain;

key.KeyChain = keychain;

key.Lock = lock;

key.Lock = lock;

dataContext.Keys.Add(key);

dataContext.Keys.Add(key);

dataContext.SaveChanges();

dataContext.SaveChanges();

 

再次,只是想知道是否有任何最佳做法或警告当遇到上述情况时。

Again, just wondering if there are any best practices or caveats when encountering the scenario above.

任何见解都表示赞赏!

推荐答案

W Brian,

 

您可以通过调用DbSet<> .Add或添加到另一个实体的导航
属性来向上下文添加实体添加(或将被添加)到上下文。 
最终,这些机制中的每一个都会导致相同的事情—一个不优先于另一个。 
在某些事情发生时会有一些差异—例如,如果你将一个实体添加到另一个实体的集合中,它(通常)不会被上下文立即跟踪。 
这是因为,对于简单的POCO实体,当您将实体添加到集合时,不会对上下文进行调用。 
但是,大多数对上下文的调用(包括SaveChanges)将在执行任何其他操作之前检测图中的新实体,此时新实体将添加到上下文并进行跟踪。 
希望有意义!  (如果没有,它可能不重要—带回家的消息是你可以使用任何一种方式。)

You can add entities to the context either by calling DbSet<>.Add or by adding to a navigation property of another entity that is added (or will be added) to the context.  Ultimately each of these mechanisms will result in the same thing—one is not preferred over the other.  There some differences in when certain things happen—for example, if you add an entity to a collection on another entity it will (usually) not be tracked by the context immediately.  This is because, with simple POCO entities, no call is being made to the context when you add the entity to the collection.  However, most calls to the context (include SaveChanges) will detect new entities in your graph before doing anything else, at which point the new entity is added to the context and tracked.  Hope that made sense!  (If not, it probably doesn’t matter—the take home message is that you can use either way.)

 

关于初始化集合,如果在EF尝试向其添加
实体时未初始化,EF将为您创建一个新集合。 
用于此初始化的类型取决于声明的collection属性的类型。对于ICollection< T> EF将使用HashSet< T>。

 

谢谢,

亚瑟


这篇关于CTP5:向上下文添加实体的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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