我正在尝试对数据库上下文运行LINQ查询:
IList<meeting> meetings;
var festivalRange = new
{
Start = festivalStartDate.Date,
End = festivalStartDate.Date.AddDays(festivalLength - 1)
};
using(CamelotViewsStandardContext db = new CamelotViewsStandardContext() )
{
meetings = db.meetings.Include("races").Where(x => x.courseId == 10 && x.meetingDate >= festivalRange.Start && x.meetingDate <= festivalRange.End).OrderBy(x => x.meetingDate).ToList();
}
无论我尝试执行哪种查询,都会出现以下错误:
Exception thrown: 'System.Data.SqlClient.SqlException' in
Microsoft.EntityFrameworkCore.dll
An exception of type 'System.Data.SqlClient.SqlException' occurred in
Microsoft.EntityFrameworkCore.dll but was not handled in user code
Invalid column name 'courseId1'.
我的数据库表中没有映射到实体模型的“ courseId1”。
这是我的“会议”模型构建者和模型:
//Meeting
modelBuilder.Entity<meeting>(entity =>
{
entity.ToTable("meeting_vw");
entity.HasMany(x => x.races).WithOne(c => c.meeting);
entity.HasKey(x => x.courseId);
entity.HasKey(x => x.meetingDate);
});
public class meeting : IMeetingKey
{
public DateTime meetingDate { get; set; }
/// <summary>
///The ID of the course of the meeting and is part of the primary key
///</summary>
///<value>Above 0</value>
///<returns>Integer</returns>
///<remarks>Part of the primary key (PK)</remarks>
///<permission>Standard</permission>
public int courseId { get; set; }
/// <summary>
///The full name of the course that of the meeting
///</summary>
///<value>Upper case</value>
///<returns>String</returns>
///<remarks>An example: HAYDOCK PARK</remarks>
///<permission>Standard</permission>
public string courseName { get; set; }
/// <summary>
///The type of surface that is raced on at the meeting
///</summary>
///<value>Title case</value>
///<returns>String</returns>
///<remarks>Values are All Weather, Turf or Both</remarks>
///<permission>Standard</permission>
public string meetingSurfaceName { get; set; }
/// <summary>
///The one character representation of the type of surface that is raced on at the meeting
///</summary>
///<value>One char</value>
///<returns>String(1)</returns>
///<remarks>Values are A - All Weather, T - Turf, B - Both</remarks>
///<permission>Standard</permission>
public string meetingSurfaceChar { get; set; }
/// <summary>
///The status of the meeting
///</summary>
///<value>"" If no status is available, title case if it is</value>
///<returns>String</returns>
///<remarks>"" - no status available, an example: Abandoned</remarks>
///<permission>Standard</permission>
public string meetingStatus { get; set; }
/// <summary>
///The type of racing
///</summary>
///<value>Title Case</value>
///<returns></returns>
///<remarks>Values are Flat, Jump, Both</remarks>
///<permission>Standard</permission>
public string meetingRaceType { get; set; }
/// <summary>
///The suffix of the meeting number as per the racing calendar
///</summary>
///<value>"" if no suffix is available, upper case if it is</value>
///<returns>String</returns>
///<remarks>"" - no suffix, an example: B</remarks>
///<permission>Standard</permission>
public string meetingNumberSuffix { get; set; }
/// <summary>
///The meeting number as per the racing calendar
///</summary>
///<value>0 if no number is available, positive integer if it is</value>
///<returns>Integer</returns>
///<remarks>0 - no meeting number, an example 255</remarks>
///<permission>Standard</permission>
public int meetingNumber { get; set; }
/// <summary>
///The time that any inspection is due to take place
///</summary>
///<value>1900-01-01 00:00:00.000 if no inspection is to take place, valid date time if it is</value>
///<returns>Date Time</returns>
///<remarks>1900-01-01 00:00:00.000 - no inspection, an example 2015-06-01 10:30:27.234</remarks>
///<permission>Standard</permission>
public DateTime inspectionTime { get; set; }
/// <summary>
///The official going report for the meeting
///</summary>
///<value>"" if no report is available, title case if it is</value>
///<returns>String</returns>
///<remarks>"" - no report, </remarks>
///<permission>Standard</permission>
public string meetingGoing { get; set; }
/// <summary>
///Internal Publish Flag
///</summary>
///<value>Internal Use Only</value>
///<returns>Integer</returns>
///<remarks>Internal Use Only</remarks>
///<permission>Standard</permission>
public int publishFlag { get; set; }
public course course { get; set; }
public List<race> races { get; set; } = new List<race>();
}
我开始认为这与模型的“竞赛”链接有关。
这是种族模型构建者和模型:
//Races
modelBuilder.Entity<race>(entity =>
{
entity.ToTable("race_standard_vw");
entity.HasKey(x => x.meetingDate);
entity.HasKey(x => x.courseId);
entity.HasKey(x => x.raceNumber);
});
public class race : IRaceKey
{
/// <summary>
///The date of the race and part of the primary key
///</summary>
///<value>Not Null</value>
///<returns>Date</returns>
///<remarks>Date of the race in the format yyyy-mm-dd</remarks>
///<permission>Standard</permission>
public DateTime meetingDate { get; set; }
/// <summary>
///The ID of the course that the race took place at and is part of the primary key
///</summary>
///<value>Above 0</value>
///<returns>Integer</returns>
///<remarks>Part of the primary key (PK)</remarks>
///<permission>Standard</permission>
public int courseId { get; set; }
/// <summary>
///The number of the race and is part of the primary key
///</summary>
///<value>1 or higher</value>
///<returns>Integer</returns>
///<remarks>Part of the primary key (PK)</remarks>
///<permission>Standard</permission>
public int raceNumber { get; set; }
/// <summary>
///The full name of the course
///</summary>
///<value>Upper Case</value>
///<returns>String</returns>
///<remarks>An example: HAYDOCK PARK</remarks>
///<permission>Standard</permission>
public string courseName { get; set; }
/// <summary>
///The 3 character course abbreviation
///</summary>
///<value>Title Case</value>
///<returns>String(3)</returns>
///<remarks>An example: Hay</remarks>
///<permission>Standard</permission>
public string courseAbbrev { get; set; }
/// <summary>
///The time that the race was due to start where the race was being run
///</summary>
///<value>yyyy-mm-ddd hh:mm:ss:ms</value>
///<returns>Date Time</returns>
///<remarks>Defaults to 1990-01-01 00:00:00.000 if not available, an example: 2012-02-19 13:10:00.000</remarks>
///<permission>Standard</permission>
public DateTime startTimeLocalScheduled { get; set; }
/// <summary>
///The time that the race was due to start at GMT
///</summary>
///<value>yyyy-mm-ddd hh:mm:ss:ms</value>
///<returns>Date Time</returns>
///<remarks>Defaults to 1990-01-01 00:00:00.000 if not available, an example: 2012-02-19 13:10:00.000</remarks>
///<permission>Standard</permission>
public DateTime startTimeGMTScheduled { get; set; }
/// <summary>
///The time that the race actually started where the race was being run
///</summary>
///<value>yyyy-mm-ddd hh:mm:ss:ms</value>
///<returns>Date Time</returns>
///<remarks>Defaults to 1990-01-01 00:00:00.000 if not available, an example: 2012-02-19 13:10:00.000</remarks>
///<permission>Standard</permission>
public DateTime actualTimeLocalScheduled { get; set; }
/// <summary>
///The time that the race actually started at GMT
///</summary>
///<value>yyyy-mm-ddd hh:mm:ss:ms</value>
///<returns>Date Time</returns>
///<remarks>Defaults to 1990-01-01 00:00:00.000 if not available, an example: 2012-02-19 13:10:00.000</remarks>
///<permission>Standard</permission>
public DateTime actualTimeGMTScheduled { get; set; }
/// <summary>
///The type of surface that is being run on
///</summary>
///<value>Title Case</value>
///<returns>String</returns>
///<remarks>Values are All Weather or Turf </remarks>
///<permission>Standard</permission>
public string raceSurfaceChar { get; set; }
/// <summary>
///The one character representation of the type of surface of the race
///</summary>
///<value>Title Case</value>
///<returns>String</returns>
///<remarks>Values are A or T </remarks>
///<permission>Standard</permission>
public string raceSurfaceName { get; set; }
/// <summary>
///The type of racing
///</summary>
///<value>Title Case</value>
///<returns>String</returns>
///<remarks>Values are Flat, Hurdle, Chase, Bumper</remarks>
///<permission>Standard</permission>
public string raceType { get; set; }
/// <summary>
///The number of complete furlongs the race will be run over
///</summary>
///<value>Above 0</value>
///<returns>Integer</returns>
///<remarks>an example: 8</remarks>
///<permission>Standard</permission>
public int distanceFurlongs { get; set; }
/// <summary>
///The number of yards the race will be run over in addition to the furlongs
///</summary>
///<value>May be 0 if the race is at an exact furlong distance</value>
///<returns>Integer</returns>
///<remarks>an example: 212</remarks>
///<permission>Standard</permission>
public int distanceYards { get; set; }
/// <summary>
///The race distance in decimal furlongs
///</summary>
///<value>Above 0.0</value>
///<returns>Decimal</returns>
///<remarks>an example: 8.51</remarks>
///<permission>Standard</permission>
public decimal distance { get; set; }
/// <summary>
///The race distance as text format
///</summary>
///<value>Display text for the distance of the race</value>
///<returns>String</returns>
///<remarks>an example: 1M 4F 50y</remarks>
///<permission>Standard</permission>
public string distanceText { get; set; }
/// <summary>
///The full name of the race
///</summary>
///<value>Upper Case</value>
///<returns>String</returns>
///<remarks>An Example: BETFRED CHELTENHAM GOLD CUP (Grade 1)</remarks>
///<permission>Standard</permission>
public string raceTitle { get; set; }
/// <summary>
///The short name of the race
///</summary>
///<value>Title case</value>
///<returns>String</returns>
///<remarks>An Example: Cheltenham Gold Cup</remarks>
///<permission>Standard</permission>
public string raceTitleShort { get; set; }
/// <summary>
///The Timeform going of the race
///</summary>
///<value>"" if no report is available, char if it is</value>
///<returns>String</returns>
///<remarks>Gd/Frm, Good, Soft, Gd/Sft</remarks>
///<permission>Standard</permission>
public string going { get; set; }
/// <summary>
///The Timeform single character going abbreviation
///</summary>
///<value>"" if no report is available, char if it is</value>
///<returns>String(1)</returns>
///<remarks>m, g, s, d</remarks>
///<permission>Standard</permission>
public string goingAbbrev { get; set; }
/// <summary>
///The official going of the race
///</summary>
///<value>"" if no report is available, Title Case if it is</value>
///<returns>String</returns>
///<remarks>"" - no report, </remarks>
///<permission>Standard</permission>
public string goingOfficial { get; set; }
/// <summary>
///The maximum age eligibility of the race
///</summary>
///<value>Can be an integer or a char</value>
///<returns>String(1)</returns>
///<remarks>If integer then that is the explicit age max, + = no max age, O - limited to the min age</remarks>
///<permission>Standard</permission>
public string eligibilityAgeMax { get; set; }
/// <summary>
///The minimum age eligibility of the race
///</summary>
///<value>Above 0</value>
///<returns>Integer</returns>
///<remarks>An example: 4</remarks>
///<permission>Standard</permission>
public int? eligibilityAgeMin { get; set; }
/// <summary>
///The number of runners either currently entered or that ran in the race
///</summary>
///<value>If 0, the value hasnt yet been calculated, above 0 if it has</value>
///<returns>Integer</returns>
///<remarks>0 - runners not yet known, an example: 5</remarks>
///<permission>Standard</permission>
public int numberOfRunners { get; set; }
/// <summary>
///The number of horses that will qualify as being placed under standard bookmaking terms
///</summary>
///<value>Above 0</value>
///<returns>Integer</returns>
///<remarks>An example: 3</remarks>
///<permission>Standard</permission>
public int numberOfPlaces { get; set; }
/// <summary>
///Details of a sex limit on the race
///</summary>
///<value>"" if no limit is involved</value>
///<returns>String(2)</returns>
///<remarks>"" means there is no sex limit, F - fillies, M - mares, C - colts, G - Geldered</remarks>
///<permission>Standard</permission>
public string eligibilitySexLimit { get; set; }
/// <summary>
///The current status of the race
///</summary>
///<value>"" - no status available, title case if it is</value>
///<returns>String</returns>
///<remarks>"" - no status available, an example: "Weighed In"</remarks>
///<permission>Standard</permission>
public string resultsStatus { get; set; }
/// <summary>
///Character value for the type of race
///</summary>
///<value>Upper Case</value>
///<returns>String(1)</returns>
///<remarks>N - Bumper, F - Flat, H - Hurdle, C - Chase</remarks>
///<permission>Standard</permission>
public string raceTypeChar { get; set; }
/// <summary>
///Representation of the flavour of race (handicap, group etc)
///</summary>
///<value>"" If not available, upper case if it is</value>
///<returns>String(2)</returns>
///<remarks>"" - not available, an example: H</remarks>
///<permission>Standard</permission>
public string raceCode { get; set; }
/// <summary>
///Total prize fund of the race
///</summary>
///<value>0.0 if not available, above 0.0 if it is </value>''
///<returns>Decimal</returns>
///<remarks>0.0 - not available, an example 9000.00</remarks>
///<permission>Standard</permission>
public decimal prizeFund { get; set; }
/// <summary>
///Prize fund for the winner of the race
///</summary>
///<value>0.0 if not available, above 0.0 if it is </value>
///<returns>Decimal</returns>
///<remarks>0.0 - not available, an example 9000.00</remarks>
///<permission>Standard</permission>
public decimal prizeFundWinner { get; set; }
/// <summary>
///The Timeform Rating of Winner of the race 5 runnings ago
///</summary>
///<value>0 if not available, above 0 if it is</value>
///<returns>Integer</returns>
///<remarks>0 - not available, an example: 134</remarks>
///<permission>Premium</permission>
public int trw1 { get; set; }
/// <summary>
///The Timeform Rating of Winner of the race 4 runnings ago
///</summary>
///<value>0 if not available, above 0 if it is</value>
///<returns>Integer</returns>
///<remarks>0 - not available, an example: 134</remarks>
///<permission>Premium</permission>
public int trw2 { get; set; }
/// <summary>
///The Timeform Rating of Winner of the race 3 runnings ago
///</summary>
///<value>0 if not available, above 0 if it is</value>
///<returns>Integer</returns>
///<remarks>0 - not available, an example: 134</remarks>
///<permission>Premium</permission>
public int trw3 { get; set; }
/// <summary>
///The Timeform Rating of Winner of the race 2 runnings ago
///</summary>
///<value>0 if not available, above 0 if it is</value>
///<returns>Integer</returns>
///<remarks>0 - not available, an example: 134</remarks>
///<permission>Premium</permission>
public int trw4 { get; set; }
/// <summary>
///The Timeform Rating of Winner of the race last time
///</summary>
///<value>0 if not available, above 0 if it is</value>
///<returns>Integer</returns>
///<remarks>0 - not available, an example: 134</remarks>
///<permission>Premium</permission>
public int trw5 { get; set; }
/// <summary>
///The Timeform Weight For Age
///</summary>
///<value>"" - not available</value>
///<returns>String</returns>
///<remarks>Example: TWFA 3 9-4 TWFA 4 9-13 </remarks>
///<permission>Premium</permission>
public string tfwfa { get; set; }
/// <summary>
///The premium post-race review of the race
///</summary>
///<value>"" if the comment hasn't been written, Title Case if it has</value>
///<returns>String</returns>
///<remarks>"" - no comment written, an example: "Winner impressive, others disapointed"</remarks>
///<permission>Premium</permission>
public string perspectiveComment { get; set; }
/// <summary>
///The Timeform Analyst pre-race predictions for the race
///</summary>
///<value>"" if not available, Title Case if it is</value>
///<returns>String</returns>
///<remarks>"" - not available, an example: HURRICANE FLY looks certain to take this race</remarks>
///<permission>Standard</permission>
public string analystVerdict { get; set; }
/// <summary>
///The comment for flat racing which is based on analysis of previous races at this course over the same distance
///</summary>
///<value>"" if not available, Title Case if it is</value>
///<returns>String</returns>
///<remarks>"" - not available, an example: Strongly favours low</remarks>
///<permission>Standard</permission>
public string drawComment { get; set; }
/// <summary>
///Information about in play records at the course
///</summary>
///<value>"" if not available, title case if it is</value>
///<returns>String</returns>
///<remarks>"" - not available, an example: Dundalk ranked 18 of 21 flat Irish courses for horses beaten at 1.01</remarks>
///<permission>Standard</permission>
public string ipHintsGeneral { get; set; }
/// <summary>
///Information about horses in the race based on their past trading and run style information
///</summary>
///<value>"" if not available, title case if it is</value>
///<returns>String</returns>
///<remarks>"" - not available, an example: Buywise traded at 1.50 on its last start when losing but travelling strongly in rear</remarks>
///<permission>Standard</permission>
public string ipHintsPriceRunStyle { get; set; }
/// <summary>
///Information about horses in the race based on their past trading information
///</summary>
///<value>"" if not available, title case if it is</value>
///<returns>String</returns>
///<remarks>"" - not available, an example: Milarrow traded at more than 5 times BSP twice when winning during its last 5 starts., Celtus has traded at 50% or less of BSP 4 times on its last 5 starts. </remarks>
///<permission>Standard</permission>
public string iPriceHistory { get; set; }
/// <summary>
///A prediction on the likely pace of the race
///</summary>
///<value>"" if not available, title case if it is</value>
///<returns>String</returns>
///<remarks>"" - not available, an example: Strongly contested </remarks>
///<permission>Standard</permission>
public string ipHintsOverallPace { get; set; }
/// <summary>
///A prediction on the likely shape of the race
///</summary>
///<value>"" if not available, title case if it is</value>
///<returns>String</returns>
///<remarks>"" - not available, an example: Kruzhlinin is unlikely to get things its own way close up and could therefore prove vulnerable in the finish: With no shortage of likely pace-forcers, Parsnip Pete may have a running style that suits. </remarks>
///<permission>Standard</permission>
public string ipHintsSpecificPace { get; set; }
/// <summary>
///An integer value of the race state
///</summary>
///<value>0 - unknown, above 0 is a valid state</value>
///<returns>Integer</returns>
///<remarks>0 - unknown, an example 22 - weighed in</remarks>
///<permission>Standard</permission>
public int raceStateId { get; set; }
/// <summary>
///For handicaps the lower handicap mark for a horse to qualify to run
///</summary>
///<value>0 if not applicable, above 0 for a valid mark</value>
///<returns>Integer</returns>
///<remarks>0 - limit not applicable, an example 130</remarks>
///<permission>Standard</permission>
public int ratingLimitLower { get; set; }
/// <summary>
///For handicaps the upper handicap mark for a horse to qualify to run
///</summary>
///<value>0 if not applicable, above 0 for a valid mark</value>
///<returns>Integer</returns>
///<remarks>0 - limit not applicable, an example 130</remarks>
///<permission>Standard</permission>
public int ratingLimitUpper { get; set; }
/// <summary>
///The class of race
///</summary>
///<value>"" if not available, title case if it is</value>
///<returns>String</returns>
///<remarks>"" if not available, an example 2</remarks>
///<permission>Standard</permission>
public string raceClass { get; set; }
/// <summary>
///The highest priority stat for the race
///</summary>
///<value>"" if not available, title case if it is with standard formatting values - # separate, * bold</value>
///<returns>String</returns>
///<remarks>"" - not available, an example: #34%#James Doyle's strike rate on favourites since 01/01/2009 (rides *TIME CHECK*)</remarks>
///<permission>Standard</permission>
public string smartStat1 { get; set; }
/// <summary>
///The second highest priority stat for the race
///</summary>
///<value>"" if not available, title case if it is with standard formatting values - # separate, * bold</value>
///<returns>String</returns>
///<remarks>"" - not available, an example: #34%#James Doyle's strike rate on favourites since 01/01/2009 (rides *TIME CHECK*)</remarks>
///<permission>Standard</permission>
public string smartStat2 { get; set; }
/// <summary>
///The third highest priority stat for the race
///</summary>
///<value>"" if not available, title case if it is with standard formatting values - # separate, * bold</value>
///<returns>String</returns>
///<remarks>"" - not available, an example: #34%#James Doyle's strike rate on favourites since 01/01/2009 (rides *TIME CHECK*)</remarks>
///<permission>Standard</permission>
public string smartStat3 { get; set; }
/// <summary>
///The Betfair market ID of this race if mapped
///</summary>
///<value>"" if not mapped to a Betfair market, title case if it is</value>
///<returns>String</returns>
///<remarks>"" - no market mapped, an example: 12321 </remarks>
///<permission>Standard</permission>
public string bfMarketId { get; set; }
/// <summary>
///The finishing time of the race in seconds
///</summary>
///<value>0 if the time in unavailable</value>
///<returns>Decimal</returns>
///<remarks>0 - if no time available, an example: 130.52</remarks>
///<permission>Standard</permission>
public decimal finishingTime { get; set; }
/// <summary>
///The time it took the leader at the point the sectional was taken to complete the race
///</summary>
///<value>0 if the time in unavailable</value>
///<returns>Decimal</returns>
///<remarks>0 - if unavailable, measured in seconds to two decimal places</remarks>
///<permission>Premium</permission>
public decimal leaderSectional { get; set; }
public decimal winnerSectional { get; set; }
public decimal distanceSectional { get; set; }
public decimal sectionalFinishingTime { get; set; }
// Additional fields MDB 2016-01-08
public int courseExtraId { get; set; }
public string distanceType { get; set; }
public string tv { get; set; }
public int perspectiveNumber { get; set; }
public int hotRace { get; set; }
public string raceId { get; set; }
public int numberOfFencesJumped { get; set; }
/// <summary>
///Internal Publish Flag
///</summary>
///<value>Internal Use Only</value>
///<returns>Integer</returns>
///<remarks>Internal Use Only</remarks>
///<permission>Standard</permission>
[JsonIgnore]
public int publishFlag { get; set; }
public string paceMapsIdealPosition { get; set; }
public string resultNotes { get; set; }
public virtual meeting meeting { get; set; }
public virtual List<entry> entries { get; set; } = new List<entry>();
public virtual List<performance> performances { get; set; } = new List<performance>();
}
任何帮助将不胜感激。谢谢。
答案 0 :(得分:2)
问题在于复合PK映射。
对于opt_expiry_dt
:
Meetings
,也适用于entity.HasKey(x => x.courseId);
entity.HasKey(x => x.meetingDate);
:
Races
此处未显示的其他实体也可能有类似的问题。
好像您期望entity.HasKey(x => x.meetingDate);
entity.HasKey(x => x.courseId);
entity.HasKey(x => x.raceNumber);
是可加的。但是实际上,它是覆盖的,因此每个HasKey
都会覆盖先前的设置,并完全将PK重新定义为为呼叫指定的PK。
如Keys (primary)文档中所示,只能使用匿名类型语法HasKey
流畅地定义组合键。对于多属性流利的映射(例如备用键,外键,索引等)也是如此。
样本实体的正确PK配置分别是
new { x.Prop1, x.Prop2, … }
和
entity.HasKey(x => new { x.courseId, x.meetingDate });