2016-03-03 99 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