2016-03-03 81 views
1

请帮忙。我不`吨明白,为什么从我的实体上下文实体框架神秘错误

var stagesExist = context.WfwDocumentWorkStages 
    .Any(it => it.Enabled && it.ExecutionId == execution.Id 
    && it.Level == execution.Level && it.ResultId == null); 

值stagesExist是假 但

var stages = context.WfwDocumentWorkStages.Where(it => it.Enabled 
    && it.ExecutionId == execution.Id 
    && it.Level == execution.Level).ToList(); 
bool stagesExist = stages.Any(it=>it.ResultId == null); 

值stagesExist是真实的?

+1

请尝试将第一个实现中的最后一个子句更改为'&& it.ResultId == null'。我认为你正在用不同的值再次捕捉它...... –

+0

这只是一个错字 – milvus

回答

0

型号:

public partial class WfwDocumentWorkScheme : EnabledEntity 
{ 
    public WfwDocumentWorkScheme() 
    { 
     this.WfwExecutionEvents = new List<WfwExecutionEvent>(); 
    } 

    public int ExecutionId { get; set; } 
    public int Level { get; set; } 
    public int? RoleId { get; set; } 
    public string CoordinatorSid { get; set; } 
    public DateTimeOffset? Date { get; set; } 
    public int? ResultId { get; set; } 
    public string Comment { get; set; } 
    public virtual Employee Coordinator { get; set; } 
    public virtual EmployeeRole EmployeeRole { get; set; } 
    public virtual WfwEventResult WfwEventResult { get; set; } 
    public virtual WfwDocumentExecution WfwDocumentExecution { get; set; } 
    public virtual ICollection<WfwExecutionEvent> WfwExecutionEvents { get; set; } 
} 

映射

public class WfwDocumentWorkSchemeMap : EntityTypeConfiguration<WfwDocumentWorkScheme> 
{ 
    public WfwDocumentWorkSchemeMap() 
    { 
     // Primary Key 
     this.HasKey(t => t.Id); 

     // Properties 
     this.Property(t => t.CoordinatorSid) 
      .HasMaxLength(46); 

     // Table & Column Mappings 
     this.ToTable("WfwDocumentWorkSchemes"); 
     this.Property(t => t.Id).HasColumnName("Id"); 
     this.Property(t => t.ExecutionId).HasColumnName("ExecutionId"); 
     this.Property(t => t.Level).HasColumnName("Level"); 
     this.Property(t => t.RoleId).HasColumnName("RoleId"); 
     this.Property(t => t.CoordinatorSid).HasColumnName("CoordinatorSid"); 
     this.Property(t => t.Date).HasColumnName("Date"); 
     this.Property(t => t.ResultId).HasColumnName("ResultId"); 
     this.Property(t => t.Comment).HasColumnName("Comment"); 
     this.Property(t => t.Enabled).HasColumnName("Enabled"); 

     // Relationships 
     this.HasRequired(t => t.Coordinator) 
      .WithMany(t => t.WfwDocumentWorkSchemes) 
      .HasForeignKey(d => d.CoordinatorSid); 
     this.HasRequired(t => t.WfwDocumentExecution) 
      .WithMany(t => t.WfwDocumentWorkSchemes) 
      .HasForeignKey(d => d.ExecutionId); 
     this.HasRequired(t => t.WfwEventResult) 
      .WithMany(t => t.WfwDocumentWorkSchemes) 
      .HasForeignKey(d => d.ResultId); 
     this.HasOptional(t => t.EmployeeRole) 
      .WithMany(t => t.WfwDocumentWorkSchemes) 
      .HasForeignKey(d => d.RoleId); 
    } 
} 

结果模型包含虚拟目录

public class WfwEventResult : EnabledEntity 
{ 
    public WfwEventResult() 
    { 
     this.WfwExecutionEvents = new List<WfwExecutionEvent>(); 
     this.WfwDocumentWorkSchemes = new List<WfwDocumentWorkScheme>(); 
    } 

    public string Name { get; set; } 
    public string Description { get; set; } 
    public bool Success { get; set; } 
    public virtual ICollection<WfwExecutionEvent> WfwExecutionEvents { get; set; } 
    public virtual ICollection<WfwDocumentWorkScheme> WfwDocumentWorkSchemes { get; set; } 
} 
+0

最好修改你的原始问题,而不是把它作为答案。 – CodeThug

+0

谢谢!下次再做 – milvus

0

和SQL事件探查器显示此查询

SELECT 
CASE WHEN (EXISTS (SELECT 
    1 AS [C1] 
    FROM (SELECT 1 AS X) AS [SingleRowTable2] 
    WHERE 1 = 0 
)) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C1] 
FROM (SELECT 1 AS X) AS [SingleRowTable1] 

为什么实体框架不工作正确?

0

的问题是这条线在映射:

this.HasRequired(t => t.WfwEventResult) 

就相当于告诉了EF的相关FK列将永远null(尽管你已经与null值使其int?,并有记录) 。请记住,EF在构建SQL查询时使用元数据信息,在这种情况下,我猜查询优化器会决定此查询永远不会返回记录(类似于.Where(it => false))并生成您看到的假SQL查询。

不久 - 确保您始终向EF提供正确的信息。在这种情况下,上述改变

this.HasOptional(t => t.WfwEventResult) 

,你会看到一个不同的(真正)查询并得到正确的结果。

+0

非常感谢!活到老,学到老 – milvus