, 나는 List<State>
State
의 엔티티 객체를 가지고 있고, 각 State
개체가 다른 개체의 컬렉션을 갖도록 Licenses
, Taxes
, Bonds
, 등 등System.Data.Entity.dll (Entity Framework)에서 NullReferenceException이 발생하는 이유는 무엇입니까?
있는 모든 객체의 목록입니다 ExpiredObjects
을위한 컬렉션도있다 만료되었으며 갱신해야합니다. 웬일인지이 속성은 특정 주 (네바다 주)에서 액세스하려고 시도 할 때 NullReferenceException
을 제공하지만 실제로는 무엇인지 파악할 수 없습니다. null
값이 표시되지 않기 때문에 null
값 어딘가에.
여기에 예외가 발생했습니다. 모든 상태를 반복하고 모든 ExpiredObjects
을 표시되는 특정보기 모음에 추가합니다. 내 테스트 코드가 여전히
private List<ExpiredObject> LoadAllExpiredObjects()
{
var list = new List<ExpiredObject>();
foreach (var state in States)
{
// This tells me the state is not null, and neither is state.ExpiredObjects
Debug.WriteLine(string.Format("{0}|{1}|{2}",
state.Name, state == null, state.ExpiredObjects == null));
try
{
var x = state.ExpiredObjects;
Debug.WriteLine(x == null);
// Exception occurs on the "for" line on the Nevada state only
// Contents in for loop do not execute
foreach (var item in x)
{
Debug.WriteLine(string.Format("{0}", item));
list.Add(item);
}
// This used to be the only line of code before I started testing
// It fails on Nevada
//list.AddRange(state.ExpiredObjects);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
}
}
return list;
}
포함되어 오류의 스택 추적은 이것이다 :
A first chance exception of type 'System.NullReferenceException' occurred in System.Data.Entity.dll
Object reference not set to an instance of an object.
at System.Data.EntityKey.AddHashValue(Int32 hashCode, Object keyValue)
at System.Data.EntityKey.GetHashCode()
at System.Collections.Generic.GenericEqualityComparer`1.GetHashCode(T obj)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at System.Data.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key, EntityEntry& entry)
at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper)
at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Data.Objects.DataClasses.RelatedEnd.Merge[TEntity](IEnumerable`1 collection, MergeOption mergeOption, Boolean setIsLoaded)
at System.Data.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption)
at System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption)
at System.Data.Objects.DataClasses.RelatedEnd.Load()
at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()
at System.Data.Objects.DataClasses.EntityCollection`1.GetEnumerator()
at MyNamespace.StatesViewModel.LoadAllExpiredObjects() in C:\Users\me\Desktop\MySolution\StatesViewModel.cs:line 217
내가 네바다를 선택하고이 ExpiredObjects
에 DataGrid
을 결합 할 때 나는 또한 동일한 오류 컬렉션 (그 바인딩을 주석으로 경우 코드 작동합니다)
이 오류의 원인이 될 수있는 사람은 누구입니까?
을 가진 사람들은, 데이터 문제 DB를에서 철저한 검사를 수행해야하는 데 도움이, 해결하기 위해 나에게 3 시간 걸렸다. –
@HenkHolterman 죄송합니다. 어떤 정보가 적절한 것인지 잘 모르겠습니다. 그것은 db-first였습니다. 모든 객체 ('State','License','Tax' 등)는 그것들을 연결하는 외래 키가있는 자체 데이터베이스 테이블을 가지고 있습니다. 그들은 또한 그들 자신의 Primary Key를 가지며, 어떤 것도 상속받지 않습니다. 지금 데이터를 조사하고 있습니다. 왜 그렇게 생각하지 않았는지 확실하지 않습니다. – Rachel
@HenkHolterman 맞습니다. 문제는 데이터에있었습니다. 만료 된 객체를 가져온 프로 시저에서 EF에 null이 아닌 것으로 표시된 필드 중 하나에 대해 'null'값이 반환되었으므로 데이터베이스에서 해당 객체를 가져 오려고 할 때마다 오류가 발생합니다. 고맙습니다. 답변을 게시하면 답변을 드리겠습니다. :) – Rachel