Sql .NET MVC 5 中的查询
Sql query in .NET MVC 5
我正在尝试使用 entity framework
执行 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 时 returns 数据正常,所以我知道查询有效并且 returns 数据正确。但是,当执行上面的代码行时,出现以下错误。
The data reader is incompatible with the specified
'MyDataModel.Service'. A member of the type, 'Id', does not have a
corresponding column in the data reader with the same name
以下是我正在访问的数据模型。
服务模式
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; }
}
}
您没有从您的服务中选择 ID 列 table。尝试将其添加到 SQL 字符串中。
查询结果需要完整映射到Service
。因此,修改您的 SELECT
以包含所有 Service
列。
我正在尝试使用 entity framework
执行 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 时 returns 数据正常,所以我知道查询有效并且 returns 数据正确。但是,当执行上面的代码行时,出现以下错误。
The data reader is incompatible with the specified 'MyDataModel.Service'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name
以下是我正在访问的数据模型。
服务模式
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; }
}
}
您没有从您的服务中选择 ID 列 table。尝试将其添加到 SQL 字符串中。
查询结果需要完整映射到Service
。因此,修改您的 SELECT
以包含所有 Service
列。