简体   繁体   中英

C# Entity Framework Error Converting nvarchar to int by enum

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; } 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.

I have managed to solve this. The answer was to remove the discriminator column i created, and allow EF to determine the type by including the type in the linq query as follows:

var test = _context.Requests.Where(r => r.EmployeeID == employeeId).OfType<AbsenceRequest>();

The OfType<>() seems to tell EF to look at the Discriminator column for this type name and only return data of that type.

It looks like you have an extra column in there "Discriminator1". Did you originally have that field as a string, then tried to convert it to an enum? The best option might be to try dropping the field altogether and make sure it drops both Discriminator and Discriminator1. Then try adding it back. This can occur in when having custom mappings, so something may be off there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM