本文介绍了如何在没有获取FirstChance IndexOutofRangeException的情况下在EF业务对象中包含[NotMapped]属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我的XAF WinForms EF应用程序加载缓慢的细节视图.然后,我学习了如何捕获FirstChance异常,并发现我遇到了IndexOutOfRange异常,如

I was wondering why my XAF WinForms EF application was slow loading a detail view.Then I learned how to capture FirstChance Exceptions and discovered I was experiencing an IndexOutOfRange exception as described here

有时候我想在我的业务对象(例如Job)中包括一个非映射属性.

Sometimes I want to include a non mapped property in my business object such as Job in example.

  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类中获取数据

And I have a method to get the data inside the 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();
        }
    }

但是Job属性发生IndexOutOfRange异常.

Yet the IndexOutOfRange exception occurs for the Job property.

调用堆栈是

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

I am using EntityFramework 6.4.4 and .Net Framework 4.7.2

推荐答案

我加了

 ,null as job

到最后一条选择语句

这篇关于如何在没有获取FirstChance IndexOutofRangeException的情况下在EF业务对象中包含[NotMapped]属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 18:02