错误:无法将类型'System.Data.Entity.DbSet'隐式转换为'System.Linq.IQueryable'

时间:2015-03-19 13:02:26

标签: asp.net linq entity-framework webforms

我正在尝试遵循以下教程:ASP.NET Web窗体Web窗体和Visual Studio入门(URL:http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview)。

我的项目中有其他名称,但想法是一样的。我正在尝试为假日公园的客户创建预订系统。 我做了与教程相同的一切,但是我得到一个错误,我不知道解决方案。在教程中,我在第5步。

我得到的错误是:无法隐式转换类型' System.Data.Entity.DbSet(PortOfTroyCustomers.Models.Accommodation)'到了System.Linq.IQueryable(PortOfTroyCustomers.Accommodation)'。存在显式转换(您是否错过了演员?)

这是Accommodation.aspx.cs的代码(在教程中:ProductList.aspx.cs)这是发生错误的地方:

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

}

其他代码:

Class Accommodation代码(与教程中的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; }
}

类排序代码(与教程中的Category.cs几乎相同):

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

上下文代码:

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

你可以帮我解决这个错误吗?

1 个答案:

答案 0 :(得分:4)

当你说: -

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

编译器推断赋值右侧的表达式类型,这在C#中称为隐式类型。

现在,您正在尝试分配,这样的查询,我们System.Data.Entity.DbSet正确&amp;左边的System.Linq.IQueryable是不同的类型: -

IQueryable<Accommodation> query = _db.Accommodations;

因此,您需要一个明确的类型转换: -

IQueryable<Accommodation> query = (IQueryable<Accommodation>)_db.Accommodations;