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

无人久伴 提交于 2021-01-29 06:32:25

问题


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

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

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

Yet the IndexOutOfRange exception occurs for the Job property.

The call stack is

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)

I am using EntityFramework 6.4.4 and .Net Framework 4.7.2


回答1:


This feels like a cludge, I added

 ,null as job

to the last select statement



来源:https://stackoverflow.com/questions/65753462/how-do-i-include-a-notmapped-property-in-an-ef-business-object-without-getting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!