当我按如下方式执行代码时,我有一个带有可空列的表...
IList<IChecklistQuestionEntity> questions = repository.Where(q => q.OriginalQuestionID.HasValue);
IList<IChecklistQuestionEntity> originalQuestions = repository.Where(q => !q.OriginalQuestionID.HasValue);
...我在profiler中看到的是......
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[OriginalQuestionID] AS [OriginalQuestionID],
[Extent1].[Question] AS [Question],
FROM [dbo].[cklChecklistQuestion] AS [Extent1]
ORDER BY [Extent1].[Question] ASC
和
SELECT
CAST(NULL AS int) AS [C1],
CAST(NULL AS int) AS [C2],
CAST(NULL AS varchar(1)) AS [C3],
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
WHERE 1 = 0
EF类实现如下......
[Table("cklChecklistQuestion")]
public class ChecklistQuestionEntity
{
/// <summary>
/// Gets or sets the ID
/// </summary>
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID
{
get;
set;
}
/// <summary>
/// Gets or sets the OriginalQuestionID
/// </summary>
public int? OriginalQuestionID
{
get;
set;
}
[Required]
public string Question
{
get;
set;
}
public ICollection<ChecklistQuestionEntity> Revisions { get; set; }
}
当然,我希望在第一个查询中看到Where IS NOT NULL子句,并且在第二个a表中选择IS NULL。
我从我的搜索中使用EF 6.1.3看来这个问题已经存在但应该修复。 EF 6.2会在这个问题上给我更多的快乐吗?
编辑:我已尝试使用和不使用OriginalQuestionID属性上的虚拟关键字EDIT2:我还尝试过没有存储库和EF上下文
IList<ChecklistQuestionEntity> questions = context.Set<ChecklistQuestionEntity>().Where(q => q.OriginalQuestionID.HasValue).ToList();
IList<ChecklistQuestionEntity> originalQuestions = context.Set<ChecklistQuestionEntity>().Where(q => !q.OriginalQuestionID.HasValue).ToList();
并得到了同样的结果。
表的创建看起来像......
CREATE TABLE [dbo].[cklChecklistQuestion](
[ID] [int] IDENTITY(1,1) NOT NULL,
[OriginalQuestionID] [int] NULL,
[Question] [nvarchar](450) NOT NULL,
CONSTRAINT [PK_cklChecklistQuestion] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[cklChecklistQuestion] WITH CHECK ADD CONSTRAINT [FK_cklChecklistQuestion_cklChecklistQuestion] FOREIGN KEY([OriginalQuestionID])
REFERENCES [dbo].[cklChecklistQuestion] ([ID])
GO
ALTER TABLE [dbo].[cklChecklistQuestion] CHECK CONSTRAINT [FK_cklChecklistQuestion_cklChecklistQuestion]
GO
EDIT3:我认为这篇文章建议应该修复
How can i query for null values in entity framework?
EDIT4:也许它与这个问题有关...... Entity Framework Linq equals value or is null
虽然它不同,因为虽然OriginalQuestionID不是主键的一部分,但它是同一个表上主键的外键关系
EDIT5:道歉所有......当我试图在较小的应用程序中复制此问题时,我发现我遗漏了一个关键部分。我修改了上面的代码以包含Revisions属性。另外,您需要以下背景。
public class MyContext : DbContext
{
public MyContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
protected MyContext()
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Commenting out the following resolves the issue
modelBuilder.Entity<ChecklistQuestionEntity>()
.HasRequired(cq => cq.OriginalQuestion)
.WithMany(cq => cq.Revisions)
.HasForeignKey(cq => cq.OriginalQuestionID)
.WillCascadeOnDelete(false);
}
}
答案 0 :(得分:1)
确定它不是真正的解决方案,但问题是由添加Revisions导航属性引起的。删除此语句可解决此问题。
modelBuilder.Entity<ChecklistQuestionEntity>()
.HasRequired(cq => cq.OriginalQuestion)
.WithMany(cq => cq.Revisions)
.HasForeignKey(cq => cq.OriginalQuestionID)
.WillCascadeOnDelete(false);
我认为这是一个错误,它与此处讨论的问题有关...
Entity Framework Linq equals value or is null
虽然OriginalQuestionID不是表中键的一部分,但它与表的主键相关,似乎会导致EF不再相信它可以为空。
我尝试升级到EF 6.2,问题仍然存在。
我唯一的解决方法是删除Revisions导航属性。