我为数据库中的视图创建了EF Code First Entity。我设置了一个测试,访问代码在我的测试中工作,但是当我在测试之外运行代码时,它会抛出“Object Reference not set to a object of object”。我被卡住了,因为我无法理解可能导致问题的原因。关于它为什么会抛出错误的任何想法?
这是我的实体类:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PhoenixData.Models.DB
{
public partial class VW_ICD10MappingWithUsage
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ProviderID { get; set; }
[StringLength(20)]
public string DxKey { get; set; }
[StringLength(100)]
public string Icd9Description { get; set; }
[StringLength(10)]
public string Code { get; set; }
[StringLength(2000)]
public string Description { get; set; }
public int? USAGE { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(20)]
public string Region { get; set; }
public long? ProviderCodeCount { get; set; }
[NotMapped]
public IList<CodeProviderUsage> ChildUsages { get; set; }
public VW_ICD10MappingWithUsage()
{
ChildUsages = new List<CodeProviderUsage>();
}
}
}
以下是引发错误的访问代码:
using (var dbContext = new MyDatabase(_config.ConnectionString))
{
res = dbContext.VW_ICD10MappingWithUsage
.Where(x => x.ProviderID == providerId)
.OrderBy(x => x.DxKey)
.Take(pageSize)
.ToList();
}
这是堆栈跟踪:
at System.Object.GetType()
at System.Data.Entity.Core.EntityKey.ValidateTypeOfKeyValue(MetadataWorkspace workspace, EdmMember keyMember, Object keyValue, Boolean isArgumentException, String argumentName)
at System.Data.Entity.Core.EntityKey.ValidateEntityKey(MetadataWorkspace workspace, EntitySet entitySet, Boolean isArgumentException, String argumentName)
at System.Data.Entity.Core.Objects.ObjectStateManager.CheckKeyMatchesEntity(IEntityWrapper wrappedEntity, EntityKey entityKey, EntitySet entitySetForType, Boolean forAttach)
at System.Data.Entity.Core.Objects.ObjectStateManager.AddEntry(IEntityWrapper wrappedObject, EntityKey passedKey, EntitySet entitySet, String argumentName, Boolean isAdded)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at PhoenixData.Core.ICD10.ICD10MappingRepository.SearchIcd10MappingWithUsages(Int32 pageSize, Int32 pageNumber, Int32 providerId, String orderByColumnName, String direction, String searchTerm, String searchColumns) in C:\Dev\PhoenixDriveServices\PhoenixData\Core\ICD10\ICD10MappingRepository.cs:line 149
答案 0 :(得分:3)
OP接受的答案是:
那么一条特定记录是否有可能在实现时导致问题?例如,您的一个列在模型中不可为空,但它在数据库中可以为空,实际值为null会混淆实现?换句话说,尝试将问题缩小到数据库中的特定行。
看起来就是这样。其中一行的复合键列之一为空。