LINQ Complied queries giving error : There is no implicit reference conversion from 'Chinook.ChinookModel' to 'System.Data.Entity.Core.Objects.ObjectContext'.


I have a class" Employee" and DBcontext : ChinookModel (.Net 4.5) , however when I try to create compiled query it gives the following error : ( Any help is appreciated, Thanks in advance )

The type 'Chinook.ChinookModel' cannot be used as type parameter 'TArg0' in the generic type or method 'System.Data.Entity.Core.Objects.CompiledQuery.Compile<TArg0,TArg1,TResult>(System.Linq.Expressions.Expression<System.Func<TArg0,TArg1,TResult>>)'. There is no implicit reference conversion from 'Chinook.ChinookModel' to 'System.Data.Entity.Core.Objects.ObjectContext'.
C:\Users\Bond009\documents\visual studio 2013\Projects\Chinook\Chinook\DAL\Queries.cs
I am using the following code to create LINQ complied query:

namespace Chinook
    public class StaticQueries
        static readonly Func<ChinookModel, int, IQueryable<Employee>> getemployeebyid =
           CompiledQuery.Compile<ChinookModel, int, IQueryable<Employee>>
           ((ctx, tempid) => from c in ctx.Employees
                             where c.EmployeeId == tempid
                             select c);

====================Employee Class ======================

    public partial class Employee
        public Employee()
            Customers = new HashSet<Customer>();
            Employee1 = new HashSet<Employee>();

        public int EmployeeId { get; set; }

        public string LastName { get; set; }

        public string FirstName { get; set; }

        public string Title { get; set; }

        public int? ReportsTo { get; set; }

        public DateTime? BirthDate { get; set; }

        public DateTime? HireDate { get; set; }

        public string Address { get; set; }

        public string City { get; set; }

        public string State { get; set; }

        public string Country { get; set; }

        public string PostalCode { get; set; }

        public string Phone { get; set; }

        public string Fax { get; set; }

        public string Email { get; set; }

        public virtual ICollection<Customer> Customers { get; set; }

        public virtual ICollection<Employee> Employee1 { get; set; }

        public virtual Employee Employee2 { get; set; }

=====================ChinookModel - Dbcontext =====================

public partial class ChinookModel : DbContext
        public ChinookModel()
            : base("name=Chinook")

        public virtual DbSet<Album> Albums { get; set; }
        public virtual DbSet<Artist> Artists { get; set; }
        public virtual DbSet<Customer> Customers { get; set; }
        public virtual DbSet<Employee> Employees { get; set; }
        public virtual DbSet<Genre> Genres { get; set; }
        public virtual DbSet<Invoice> Invoices { get; set; }
        public virtual DbSet<InvoiceLine> InvoiceLines { get; set; }
        public virtual DbSet<MediaType> MediaTypes { get; set; }
        public virtual DbSet<Playlist> Playlists { get; set; }
        public virtual DbSet<Track> Tracks { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
                .HasMany(e => e.Albums)
                .WithRequired(e => e.Artist)

                .HasMany(e => e.Invoices)
                .WithRequired(e => e.Customer)

                .HasMany(e => e.Customers)
                .WithOptional(e => e.Employee)
                .HasForeignKey(e => e.SupportRepId);

                .HasMany(e => e.Employee1)
                .WithOptional(e => e.Employee2)
                .HasForeignKey(e => e.ReportsTo);
        public int ThisID { get; set; }
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
The error is indicating that ChinookModel is not derived from ObjectContext.
In this case ChinookModel is derived from DbContext which is the current recommended context to use with .NET 4.5 . (see the link DBContext or Object Context which states:
    Models created in Visual Studio 2012 & 2013 will generate simple POCO entity classes and a context that derives from the simplified DbContext.
    Models created in Visual Studio 2010 will generate entity classes that derive from EntityObject and a context that derives from ObjectContext.
    Note: In Visual Studio 2010 we recommend switching to the DbContext Generator template once you've added your model.
This might have worked with earlier versions say 4.1, because EF created an object derived from ObjectContext instead of DbContext for us to manage our entities.
If you are targeting .NET 4.5 and EF (Entity Framework 5.0), we may no more need compiled queries as queries shall be cached automatically for you, as this post explains. Automatic Caching EF5
If you are not convinced, still want to use Compiled Queries, then you can switch back your ChinookModel to be derived from ObjectContext. But before that you may want to consider this article as well. Compiled or Bust?

Hope this helps,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Karrtik IyerSoftware ArchitectCommented:
May I please know why is this a grade B, I have explained the reason why error was coming and what are the possible way to solve it?
ExplorerJKAuthor Commented:
My bad ....I am new to this grading.. system....please change to A... Thanks Karrtik.
asdasd asdasdCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.