阅读本标题为“使用ObjectSet(实体框架)”的MSDN文章它显示了有关如何添加产品的两个示例..一个用于3.5,另一个用于4.0。3种通过实体框架添加“产品”的方法。有什么不同?
http://msdn.microsoft.com/en-us/library/ee473442.aspx
通过我的知识缺乏的我在这里很可能完全失去了一些东西,但我从来没有加入像这样的产品:
//In .NET Framework 3.5 SP1, use the following code: (ObjectQuery)
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Add the new object to the context.
context.AddObject("Products", newProduct);
}
//New in .NET Framework 4, use the following code: (ObjectSet)
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Add the new object to the context.
context.Products.AddObject(newProduct);
}
我不会做它无论哪种方式,只是使用:
// (My familiar way)
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Add the new object to the context.
context.AddToProducts(newProduct);
}
这三种方式有什么区别?
“我的方式”是使用ObjectQuery的另一种方式吗?
感谢, 钢钣
ObjectSet被缓存在默认的代码生成器中。 所以我猜想使用context.Products.Add会有一些轻微的性能提升,因为它可以重新使用相同的集合而不用任何查找。 – 2010-05-11 15:11:15
优秀。非常感谢您向我解释这一点。事实证明,这比我想象的要简单得多。我真的应该更频繁地检查所有这些生成的文件,以便更好地理解幕后发生的事情。我现在看一看,看看还有哪些事情正在发生! – 4imble 2010-05-11 16:17:34
*非常值得阅读codegen文件。即使你没有写,也可以随时使用这些代码。 – 2010-05-11 17:19:43