locked
Getting a reference to the AsCached property RRS feed

  • Question

  • I have my ObjectContext exposed as a public property and am able to access it via my DBContext.

    I want to enable caching for a drop down list. It requires the AsCachedproperty. However, I can't get it to display in the intellisense. Do I need to include a specific namespace for it?

    Here is my existing LINQ statement that I want cached.

    IQueryable<Category> category = DbContext.Categories.Where(p => p.CategoryID > 0);
    

    I'm tying to do something like this. Note though, that there is nointellisense when placing the "." after Objectcontext.

    IQueryable<Category> category = DbContext.Objectcontext.Categories.AsCached.Where(p => p.CategoryID > 0);
    
    The ObjectContext pops up in the intellisense, but not the AsCached property.
    How can I get the AsCached property to appear?

    Bill Yeager
    Wednesday, November 23, 2011 6:13 PM

Answers

  • Hi Bill,

    You can refer this link: http://msdn.microsoft.com/en-us/library/gg696590(v=vs.103).aspx

    >>System.Data.Entity.Infrastructure;

     In EntityFramework.dll.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bill_Yeager Thursday, December 1, 2011 12:37 AM
    Wednesday, November 30, 2011 7:32 AM
  • Hi Bill;

    As Alan stated the EntityFramework.dll needs to be loaded into the project in order to use the namespace System.Data.Entity.Infrastructure.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Bill_Yeager Thursday, December 1, 2011 12:37 AM
    Wednesday, November 30, 2011 2:12 PM

All replies

  • Hi Bill;

    Where did you see of the property AsCached ?


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, November 23, 2011 10:06 PM
  • Fernando, it's at the bottom of this link:

    http://bobhl.blogspot.com/2011/02/caching-in-entity-framework-using-query.html

     


    Bill Yeager
    Wednesday, November 23, 2011 10:24 PM
  • Hi Bill;

    In the link you posted it had a link to the page that has an, "A caching extension for IQueryable with Query Notifications invalidation support. ", This is not a Microsoft implementation and must be loaded into your project. Please go to this link linqtocache SqlDependency invalidation for LINQ queries to download the extension and to get detail on how to use it.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, November 23, 2011 10:59 PM
  • I found it, but I don't think I'm going to use it....

    I can't run the WCF Test Client app against it with that property...

    I know that caching is going to be supported in EF 4.5. For now, I think I'll just have to wait unless there is another easy implementation I can use.

     

     


    Bill Yeager
    Thursday, November 24, 2011 12:27 AM
  • Fernando, that link seems like the best way to implement the cache.

    However, it involves getting access to your ObjectContext object.

    I have my ObjectContext exposed as a public property and am able to access it via myDBContext in an EF 4.1 model.

    However, I can't get any intellisense after I put in the following:

    "DbContext.Objectcontext."

    I need to access properties and methods from my ObjectContext object. How can I do this? What am I missing?


    Bill Yeager
    Sunday, November 27, 2011 5:36 PM
  • Hi Bill;

    Try doing the following, Add a using statement to the top of your code file with the following statement, using System.Data.Entity.Infrastructure; , then to get the reference to the ObjectContext do the following:

    ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;

    Where dbContext is the DbContext instance variable. Then when you need the ObjectContext variables use the objectContext variable. Or you can also do it without assigning it to a variable as follows :

    ((IObjectContextAdapter)dbContext).ObjectContext.xxx;

    Where xxx is the properties of the ObjectContext.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, November 27, 2011 6:30 PM
  • Fernando, I tried adding the using statement above, but gives me a design time compile error that I'm missing a reference.

    When I try to add a reference for it, there is no namespace by the name System.Data.Entity.Infrastructure;

     

    Am I missing something?


    Bill Yeager
    Wednesday, November 30, 2011 1:26 AM
  • Hi Bill,

    You can refer this link: http://msdn.microsoft.com/en-us/library/gg696590(v=vs.103).aspx

    >>System.Data.Entity.Infrastructure;

     In EntityFramework.dll.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bill_Yeager Thursday, December 1, 2011 12:37 AM
    Wednesday, November 30, 2011 7:32 AM
  • Hi Bill;

    As Alan stated the EntityFramework.dll needs to be loaded into the project in order to use the namespace System.Data.Entity.Infrastructure.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Bill_Yeager Thursday, December 1, 2011 12:37 AM
    Wednesday, November 30, 2011 2:12 PM
  • Thanks Fernando & Alan...

    Got it...


    Bill Yeager
    Thursday, December 1, 2011 12:37 AM
  •  

    Not a problem Bill, glad I was able to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 1, 2011 1:02 AM
  • Fernando, I have an issue when trying to start the SqlDependency.

    The error informs me: Keyword not supported: 'metadata'.       

    The connectionstring is the following when retrieved from the immediate window right before it crashes.

    ?objectContext.Connection.ConnectionString
    "metadata=res://*/YeagerTech.csdl|res://*/YeagerTech.ssdl|res://*/YeagerTech.msl;provider=System.Data.SqlClient;provider connection string=\"data source=Bill-PC;initial catalog=YeagerTech;integrated security=True;multipleactiveresultsets=True;App=EntityFramework\""

    Here is the code. It crashes on the Start method. Apparently, it doesn't think the EF connectionstring is valid. Any idea of how I can correctly use this?

    YeagerTechEntities dbContext = new YeagerTechEntities();
     
                ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
     
                SqlDependency.Start(objectContext.Connection.ConnectionString);

    Bill Yeager
    Friday, December 2, 2011 1:07 AM