1

I am trying to follow the following tutorial: ASP.NET Web Forms Getting started 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).

I have other names in my project but the idea is the same. I am trying to create a Reservations system for customers of a Holiday Park. I do everything the same as the tutorial but I get an error where I don't know the solution of. In the tutorial I am at step 5.

The error I get is: Cannot implicitly convert type 'System.Data.Entity.DbSet(PortOfTroyCustomers.Models.Accommodation)' to 'System.Linq.IQueryable(PortOfTroyCustomers.Accommodation)'. An explicit conversion exists (are you missing a cast?)

This is the code of Accommodation.aspx.cs (In tutorial: ProductList.aspx.cs) This is where the error occurted:

     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 code:

Class Accommodation code (almost the same as Product.cs in the tutorial):

    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; }
}

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

    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; }
}

Context code:

    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"));
    }
}

Can you please help me with this error??

1 Answer 1

4

When you say:-

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

The compiler infers the type of the expression to the right of the assignment, this is known as implicit type in C#.

Now, you are trying to assign, your query like this, we have System.Data.Entity.DbSet on right & System.Linq.IQueryable on left which are different types:-

IQueryable<Accommodation> query = _db.Accommodations;

Thus, you need an explicit typecast like this:-

IQueryable<Accommodation> query = (IQueryable<Accommodation>)_db.Accommodations;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks!! That worked! :D And of toppic: I have now the error: 'PortOfTroyCustomers.Accommodation' does not contain a definition for 'SortID' and no extension method 'SortID' accepting a first argument of type 'PortOfTroyCustomers.Accommodation' could be found (are you missing a using directive or an assembly reference?). This error occurted by SortID query = query.Where(a => a.SortID == sortId);. Do you know how I can fix this error as well? I also don't know how to fix this error.
@Marjolein - I am not sure why you are converting it to IQuerable<Accommodation>, instead of that try this:- List<Accommodation> query = _db.Accommodations.ToList(); and it should solve your problem.
If I do that I get the same errors as before.. But now by _db.Accommodations.ToList() / SortID / return query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.