C#实体框架错误通过枚举将nvarchar转换为int

时间:2016-04-06 15:10:40

标签: c# entity-framework enums

我遇到以下错误时遇到问题:

Conversion failed when converting the nvarchar value 'HolidayRequest' to data type int.

我在Entity Framework中有一个继承结构。我的基本模型如下:

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

当我尝试从数据库中检索数据时,如下所示:

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

这是发生错误的地方。

鉴于错误的背景,我认为这是枚举public RequestDiscriminator Discriminator { get; set; }

的问题

Enum的结构是:

public enum RequestDiscriminator
{
    Holiday,
    Absence
}

实体框架发送的SQL语句是:

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])

任何帮助都会非常感激,因为我完全被难倒了。

更新

根据下面答案中的建议,我已从数据库中删除了两个Discriminator列并读取了我的。但是,SQL查询似乎仍在寻找现在不存在的Discriminator1列。

2 个答案:

答案 0 :(得分:2)

我设法解决了这个问题。答案是删除我创建的鉴别器列,并允许EF通过在linq查询中包含类型来确定类型,如下所示:

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

OfType&lt;&gt;()似乎告诉EF查看此类型名称的Discriminator列,并仅返回该类型的数据。

答案 1 :(得分:0)

看起来你在“Discriminator1”中有一个额外的列。你原来是将字段作为字符串,然后尝试将其转换为枚举吗?最好的选择可能是尝试完全放弃该字段并确保它同时丢弃Discriminator和Discriminator1。然后尝试将其添加回来。这可能发生在具有自定义映射时,因此某些东西可能会在那里。