none
Why is MergeOption not workinging in EF5 with VS2012? RRS feed

  • Question

  • Hi all,

    I did a search and couldn't find anything on this, but I am sure it's a really simple problem....

    I am going through the performance page from the Microsoft EF group and am working on understanding some of the performance issues. 

    In one topic (6.2) they have the following code:

    context.Products.MergeOption = MergeOption.NoTracking;
    var q = context.Products.Where(p => p.Category.CategoryName == "Beverages");

    When I try to put that in, the editor underlines the first MergeOption (as part of context.Products.MergeOption) and shows the following error message:

    Error 1 'System.Data.Entity.DbSet<businessObjects.Product>' does not contain a definition for 'MergeOption' and no extension method 'MergeOption' accepting a first argument of type 'System.Data.Entity.DbSet<businessObjects.Product>' could be found (are you missing a using directive or an assembly reference?)

    I did some research and the following code does run:

    var foo = context.Products.AsNoTracking(); var xx = foo.Where(p => p.Category.CategoryName == "Beverages");

    I am running VS2012 and believe I am running EF 5 (although I am not quite sure how to confirm the EF version.  One thread said to look for a Microsoft.ADO... file in the C:\Program Files - but I couldn't find anything there.  Nor is the EF version listed Help/About... But I downloaded it and think it's 5.

    So why is it that the sample code from the EF teams performance page doesn't work (i.e. what do I have wrong so that I get an error when I shouldn't.)

    If it's any help, I am also having a problem with the next example as well (6.3) because it doesn't like the use of a string following the where:

    ObjectQuery<Product> products = context.Products.Where("it.Category.CategoryName = 'Beverages'");
    

    It underlines everything from the context.Products to the end and complains that the DbSet doesn't contain a definition for "where" and the best extension method overload 'System.Linq.Enumerable.Where<TSource>... has invalid arguments.  I don't know if this is related or a separate issue.

    But the others (at least up through 6.4) have worked just fine.

    Thanks


    me (and yes, I DO mark correct answers)


    • Edited by FletcherJ Wednesday, April 17, 2013 12:16 AM
    Wednesday, April 17, 2013 12:01 AM

Answers

  • Hi FletcherJ,

    In fact they are from ObjectContext API. If you are referring to this article: Performance Considerations for Entity Framework 5, there is a paragraph (5.1) saying: "When querying using ObjectContext, ObjectQuery and ObjectSet instances will remember a MergeOption once it is set, and queries that are composed on them will inherit the effective MergeOption of the parent query. When using DbContext, tracking can be disabled by calling the AsNoTracking() modifier on the DbSet."

    Not all features are provided in DbContext API. Sometimes we need to access ObjectContext from the DbContext. For example:

    var objectContext = ((IObjectContextAdapter)context).ObjectContext;
    objectContext.CreateObjectSet<Products>().Where("it.Category.CategoryName = 'Beverages'");

    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 18, 2013 11:00 AM
    Moderator

All replies

  • Hi FletcherJ,

    In fact they are from ObjectContext API. If you are referring to this article: Performance Considerations for Entity Framework 5, there is a paragraph (5.1) saying: "When querying using ObjectContext, ObjectQuery and ObjectSet instances will remember a MergeOption once it is set, and queries that are composed on them will inherit the effective MergeOption of the parent query. When using DbContext, tracking can be disabled by calling the AsNoTracking() modifier on the DbSet."

    Not all features are provided in DbContext API. Sometimes we need to access ObjectContext from the DbContext. For example:

    var objectContext = ((IObjectContextAdapter)context).ObjectContext;
    objectContext.CreateObjectSet<Products>().Where("it.Category.CategoryName = 'Beverages'");

    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 18, 2013 11:00 AM
    Moderator
  • Chester,

    Thanks, I tried something similar to that.  But when I use your example (copy/paste) I get another error - with <Products> underlined in the 2nd line:

    The type or namespace name 'Products' could not be found (are you missing a using directive or an assembly reference?) 

    I right clicked on the error line, but the option to add a using isn't there.  So I am guessing that I need to preface the Products with something - but using NorthwindEntities isn't it.  Nor is context.  Any ideas what I am missing?

    I am also curious why their example doesn't work.  They simply use context.Products.Where(...) - is that syntax wrong or is it based on a context that is created in a way other than something like:

    using (var context = new NorthwindEntities())
    

    Also, any ideas about the other issues in the post? 

    I am wondering if I am somehow missing (or including a wrong) using statement.  These are the VS using statements:

    using System.Linq;
    using System.Data.Entity.Infrastructure;
    using System.Data.EntityClient;
    using System.Data;
    using System.Data.Objects;
    using System;

    Thanks 


    me (and yes, I DO mark correct answers)

    Thursday, April 18, 2013 6:02 PM
  • Hi FletcherJ,

    I'm really sorry, it's my fault. I'm not using Northwind to test the code. It should be the POCO class Product rather than Products.

    Not all the features are provided in DbContext API, including Entity SQL. LINQ to Entities doesn't provide a Where method receive a string parameter, so we cannot simply use context.Products.Where("..."). 

    There are also some features like MetadataWorkspace should be accessed by ObjectContext.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 19, 2013 1:58 AM
    Moderator