Error: Cannot implicitly convert type 'System.Data.Entity.DbSet' to 'System.Linq.IQueryable'

asp.net entity-framework linq webforms

Question

I'm attempting to adhere to the tutorial below: Web Forms for ASP.NET How to begin with Web Forms and Visual Studio (URL: http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview).

Although my project uses different names, the concept is the same. I'm attempting to develop a reservations system for visitors to a vacation park. I follow the tutorial exactly, but I encounter a problem for which I have no idea how to fix it. Step 5 is where I am in the tutorial.

Cannot implicitly convert type 'System.Data.Entity' is the message I receive. Change "System.Linq.IQueryable(PortOfTroyCustomers.Accommodation)" to "DbSet(PortOfTroyCustomers.Models.Accommodation)". There is an explicit conversion (do you need a cast?)

This is Accommodation.aspx.cs's code (In tutorial: ProductList.aspx.cs) This is where the mistake happened:

     public partial class Accommodation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public IQueryable<Accommodation> GetAccommodations([QueryString("id")] int? sortId)
    {
        var _db = new PortOfTroyCustomers.Models.PortOfTroyContext();
        IQueryable<Accommodation> query = _db.Accommodations; // This is the spot!
        if (sortId.HasValue && sortId > 0)
        {
            query = query.Where(a => a.SortID == sortId); // and this!
        }
        return query;
    }

}

Other coding

The class accommodation code is virtually identical to the tutorial's Product.cs:

    public class Accommodation
{
    [ScaffoldColumn(false)]
    public int AccommodationID { get; set; }

    public short NumberOfGuest { get; set; }

    public string ImagePath { get; set; }

    [Display(Name = "Richtprijs per week")]
    [DataType(DataType.Currency)] // data type op geld zetten voor de double
    public double? PricePerWeek { get; set; }

    // foreign key
    public int? SortID { get; set; }
    public int? ResortID { get; set; }

    // relaties leggen
    public virtual Sort Sort { get; set; }
    public virtual Resort Resort { get; set; }
}

(Almost the same as Category.cs in the tutorial) Class Sort code:

    public class Sort
{
    [ScaffoldColumn(false)]
    [Display(Name = "Type")]
    public int SortID { get; set; }

    [Required, StringLength(100), Display(Name = "Name")]
    public string Title { get; set; }

    [Required, StringLength(10000), DataType(DataType.MultilineText)]
    public string Description { get; set; }

    public virtual ICollection<Accommodation> Accommodations { get; set; }
}

Code context:

    public class PortOfTroyContext : DbContext
{
    public PortOfTroyContext() : base("PortOfTroyCustomers")
    {
    }

    public DbSet<Sort> Sorts { get; set; }
    public DbSet<Accommodation> Accommodations { get; set; }
    public DbSet<Resort> Resorts { get; set; }
    public DbSet<Facility> Facilities { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // nieuwe koppeltabel tussen Ficilities en Resorts (is namelijk een TNMT relatie)
        modelBuilder.Entity<Facility>()
            .HasMany(r => r.Resorts).WithMany(i => i.Facilities)
            .Map(t => t.MapLeftKey("FicilityID")
                .MapRightKey("ResortID")
                .ToTable("FicilityResort"));
    }
}

Could you kindly assist me with this mistake?

1
1
3/19/2015 1:02:25 PM

Accepted Answer

Once you say:

var _db = new PortOfTroyCustomers.Models.PortOfTroyContext();

Implicit type is a term used in C# to describe how the compiler infers the type of the expression to the right of the assignment.

You are attempting to assign your query right now, and we haveSystem.Data.Entity.DbSet right andSystem.Linq.IQueryable Various varieties are on the left.

IQueryable<Accommodation> query = _db.Accommodations;

So, you need a typecast that is explicit, like this:

IQueryable<Accommodation> query = (IQueryable<Accommodation>)_db.Accommodations;
4
3/19/2015 1:44:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow