如何在 EF 业务对象中包含 [NotMapped] 属性 而不会出现 FirstChance IndexOutofRangeException?

How do I include a [NotMapped] property in an EF business object without getting FirstChance IndexOutofRangeException?

我想知道为什么我的 XAF WinForms EF 应用程序加载详细视图时速度很慢。 然后我学习了如何捕获 FirstChance 异常并发现我遇到了

中描述的 IndexOutOfRange 异常

有时我想在我的业务对象中包含一个非映射 属性,例如示例中的 Job。

  public class OrderLineResult  
    {
        public int LineId { get; set; }
        public int Quantity { get; set; }
        public string Description { get; set; }
        [Browsable(false)] public int JobId { get; set; }
        [NotMapped] [Browsable(false)] public virtual Job Job { get; set; } 
    }

我有一个方法可以获取 OrderLineResult 中的数据 class

public static OrderLineResult[] GetData(int headId)
    {
        using var connect = new MyDbContext()
            const string sql =
            @"SET NOCOUNT ON;
            create table #temp( JobId int, Quantity int,  LineId int, Description   )
            /* code to populate the table */
            select JobId,LineId,Quantity, Description  from #temp"
            var results = connect.Database.SqlQuery<OrderLineResult>(sql,headId).ToArray();
            return results.ToArray();
        }
    }

然而作业 属性.

出现 IndexOutOfRange 异常

调用堆栈为

System.IndexOutOfRangeException
Job
   at MyApp.Module.Win.Controllers.ToDoList.TaskActionController.<>c.<actExceptions_Execute>b__34_0(Object sender, FirstChanceExceptionEventArgs e)
   at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
   at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
   at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.TryGetColumnOrdinalFromReader(DbDataReader storeDataReader, String columnName, Int32& ordinal)
   at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndClrType(DbDataReader reader, Type type, MetadataWorkspace workspace)
   at System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate[TElement](DbDataReader reader, String entitySetName, MergeOption mergeOption, Boolean streaming, EntitySet& entitySet, TypeUsage& edmType)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
   at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass186_0`1.<ExecuteStoreQueryReliably>b__1()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass186_0`1.<ExecuteStoreQueryReliably>b__0()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery[TElement](String commandText, ExecutionOptions executionOptions, Object[] parameters)
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at MyApp.Module.BusinessObjects.NonPersistedBusinessObjects.OrderLineResult.GetData(Int32 headId)

我正在使用 EntityFramework 6.4.4 和 .Net Framework 4.7.2

这感觉像是一个障碍, 我加了

 ,null as job

到最后一个 select 语句