【发布时间】:2015-03-27 23:32:01
【问题描述】:
我正在尝试使用实体框架执行 SQL 查询
var timetable = db.Services.SqlQuery("SELECT Services.Date, Services.Time, Trains.Name, ServiceTypes.Name FROM Services INNER JOIN Trains ON Services.TrainId = Trains.Id INNER JOIN ServiceTypes ON Services.ServiceType = ServiceTypes.Id").ToList();
在数据库上执行 SQL 时返回数据正常,因此我知道查询有效并返回正确的数据。然而,当执行上面的代码行时,我得到了以下错误。
数据读取器与指定的不兼容 'MyDataModel.Service'。类型“Id”的成员没有 数据阅读器中同名的对应列
以下是我正在访问的数据模型。
服务模式
public partial class Service
{
public Service()
{
this.Tickets = new HashSet<Ticket>();
this.TrainStops = new HashSet<ServiceStop>();
}
public int Id { get; set; }
public int TrainId { get; set; }
public Nullable<int> DisabledUserId { get; set; }
public System.TimeSpan Time { get; set; }
public System.DateTime Date { get; set; }
public int ServiceType { get; set; }
public virtual Train Train { get; set; }
public virtual ICollection<Ticket> Tickets { get; set; }
public virtual ServiceType ServiceTypeId { get; set; }
public virtual ICollection<ServiceStop> TrainStops { get; set; }
public virtual User User { get; set; }
}
}
火车模型
public partial class Train
{
public Train()
{
this.Services = new HashSet<Service>();
this.Tickets = new HashSet<Ticket>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Manufacturer { get; set; }
public int Year { get; set; }
public int Seats { get; set; }
public virtual ICollection<Service> Services { get; set; }
public virtual ICollection<Ticket> Tickets { get; set; }
}
}
服务类型模型
public partial class ServiceType
{
public ServiceType()
{
this.Services = new HashSet<Service>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Service> Services { get; set; }
}
}
【问题讨论】:
标签: c# sql asp.net-mvc entity-framework asp.net-mvc-5