troubleshooting Question

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

Avatar of Explorer JK
Explorer JK asked on
C#ASP.NET
4 Comments1 Solution434 ViewsLast Modified:
Hi,

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 )

Error
1
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
18
12
Chinook
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 ======================

    [Table("Employee")]
    public partial class Employee
    {
        public Employee()
        {
            Customers = new HashSet<Customer>();
            Employee1 = new HashSet<Employee>();
        }

        public int EmployeeId { get; set; }

        [Required]
        [StringLength(20)]
        public string LastName { get; set; }

        [Required]
        [StringLength(20)]
        public string FirstName { get; set; }

        [StringLength(30)]
        public string Title { get; set; }

        public int? ReportsTo { get; set; }

        public DateTime? BirthDate { get; set; }

        public DateTime? HireDate { get; set; }

        [StringLength(70)]
        public string Address { get; set; }

        [StringLength(40)]
        public string City { get; set; }

        [StringLength(40)]
        public string State { get; set; }

        [StringLength(40)]
        public string Country { get; set; }

        [StringLength(10)]
        public string PostalCode { get; set; }

        [StringLength(24)]
        public string Phone { get; set; }

        [StringLength(24)]
        public string Fax { get; set; }

        [StringLength(60)]
        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)
        {
            modelBuilder.Entity<Artist>()
                .HasMany(e => e.Albums)
                .WithRequired(e => e.Artist)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Customer>()
                .HasMany(e => e.Invoices)
                .WithRequired(e => e.Customer)
                .WillCascadeOnDelete(false);

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

            modelBuilder.Entity<Employee>()
                .HasMany(e => e.Employee1)
                .WithOptional(e => e.Employee2)
                .HasForeignKey(e => e.ReportsTo);
        }
        public int ThisID { get; set; }
    }
======================================================
ASKER CERTIFIED SOLUTION
Karrtik Iyer
Software Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 4 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 4 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004