I have an issue receiving the following error:
Conversion failed when converting the nvarchar value 'HolidayRequest' to data type int.
I have an inheritance structure within Entity Framework. My base Model is as follows:
public abstract class Request
{
public int ID { get; set; }
public int EmployeeID { get; set; }
public string Subject { get; set; }
[Display(Name = "Date Requested")]
public DateTime DateRequested { get; set; }
[Display(Name = "Start Date")]
public DateTime StartDateTime { get; set; }
[Display(Name = "Return To Work Date")]
public DateTime EndDateTime { get; set; }
public RequestStatus Status { get; set; }
public string Notes { get; set; }
public int? ResponseID { get; set; }
public RequestDiscriminator Discriminator { get; set; }
[ForeignKey("EmployeeID")]
public virtual Employee Employee { get; set; }
[ForeignKey("ResponseID")]
public virtual Response Response { get; set; }
}
When I attempt to retrieve data from the database as follows:
public IQueryable<Request> GetPendingHolidayRequestsByEmployeeId(int employeeId)
{
var list = _context.Requests.Where(p => p.Discriminator == RequestDiscriminator.Holiday && p.EmployeeID == employeeId && p.Status == RequestStatus.NotProcessed);
return list;
}
This is where the error occurs.
Given the context of the error I believe that it is an issue with this enum public RequestDiscriminator Discriminator { get; set; }
The structure of the Enum is:
public enum RequestDiscriminator
{
Holiday,
Absence
}
The SQL statement that is being sent by entity framework is:
SELECT
[Extent1].[ID] AS [ID],
CASE WHEN ([Extent1].[Discriminator1] = N'AbsenceRequest') THEN '0X0X' ELSE '0X1X' END AS [C1],
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[Subject] AS [Subject],
[Extent1].[DateRequested] AS [DateRequested],
[Extent1].[StartDateTime] AS [StartDateTime],
[Extent1].[EndDateTime] AS [EndDateTime],
[Extent1].[Status] AS [Status],
[Extent1].[Notes] AS [Notes],
[Extent1].[ResponseID] AS [ResponseID],
[Extent1].[Discriminator] AS [Discriminator]
CASE WHEN ([Extent1].[Discriminator1] = N'AbsenceRequest') THEN [Extent1].[ReasonCode] END AS [C2],
CASE WHEN ([Extent1].[Discriminator1] = N'AbsenceRequest') THEN [Extent1].[TakenPaid] END AS [C3],
CASE WHEN ([Extent1].[Discriminator1] = N'AbsenceRequest') THEN CAST(NULL AS float) ELSE [Extent1].[TotalDays] END AS [C4],
CASE WHEN ([Extent1].[Discriminator1] = N'AbsenceRequest') THEN [Extent1].[Employee_EmployeeID] END AS [C5],
CASE WHEN ([Extent1].[Discriminator1] = N'AbsenceRequest') THEN CAST(NULL AS int) ELSE [Extent1].[Employee_EmployeeID1] END AS [C6]
FROM [dbo].[Request] AS [Extent1]
WHERE ([Extent1].[Discriminator1] IN (N'AbsenceRequest',N'HolidayRequest')) AND (0 = [Extent1].[Discriminator]) AND (0 = [Extent1].[Status])
Any help would be greatly appreciated as i am completely stumped.
UPDATE
As per the suggestion in the answer below, I have removed both Discriminator columns from the database and readded mine. However, the SQL query still appears to be looking for the Discriminator1 column that now doesnt exist.
Discriminatoris not a good idea, especially combined with TPH. EF maintains it's ownstringcolumn calledDiscriminatorto distinguish the derived classes, so it might been hardcoded somewhere (just a guess).