【问题标题】:Entity Framework mysterious error实体框架神秘错误
【发布时间】:2016-03-03 13:00:20
【问题描述】:

请帮忙。我不明白为什么从我的实体上下文中

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

value stageExist 为 false 但是

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

value stagesExist 是真的吗??

【问题讨论】:

  • 请尝试将第一个实现中的最后一个子句更改为&& it.ResultId == null。我认为您正在以不同的值再次捕获it...
  • 这里只是一个错字

标签: entity-framework


【解决方案1】:

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]

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

【讨论】:

    【解决方案2】:

    型号:

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

    【讨论】:

    • 最好修改你原来的问题,而不是把它作为答案。
    • 谢谢!下次做
    【解决方案3】:

    问题在于您的映射中的这一行:

    this.HasRequired(t => t.WfwEventResult)
    

    您实际上是在告诉 EF 关联的 FK 列永远不会是 null(尽管您已经将其设为 int? 并且记录具有 null 值)。请记住,EF 在构建 SQL 查询时使用元数据信息,在这种情况下,我猜查询优化器决定此查询永远不会返回记录(类似于 .Where(it =&gt; false))并生成您看到的假 SQL 查询。

    很快 - 确保您始终向 EF 提供正确的信息。在这种情况下,将上面的内容更改为

    this.HasOptional(t => t.WfwEventResult)
    

    您会看到不同的(真实的)查询并得到正确的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 2017-04-20
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多