【发布时间】:2015-05-22 14:39:20
【问题描述】:
我正在尝试遵循以下教程: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;
}
}
其他代码:
类住宿代码(与教程中的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"));
}
}
你能帮我解决这个错误吗?
【问题讨论】:
标签: asp.net linq entity-framework webforms