Code First: Entity accessible in tests, but “not set to an instance of an object” outside of tests

我是研究僧i 提交于 2021-02-11 12:41:00

问题


I created an EF Code First Entity for a view in my database. I setup a test, and the access code works in my test, but when I run the code outside of the test, it throws "Object Reference not set to an instance of an object". I am stuck, as I cannot understand what could be causing the issue. Any ideas on why it's throwing an error?

Here is my entity class:

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

Here is access code that throws the error:

using (var dbContext = new MyDatabase(_config.ConnectionString))
{
    res = dbContext.VW_ICD10MappingWithUsage
    .Where(x => x.ProviderID == providerId)
    .OrderBy(x => x.DxKey)
    .Take(pageSize)
    .ToList();
}

And here is the stack trace:

   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

回答1:


The answer accepted by the OP is:

Is it possible then that one specific record causes the issue upon materialization? For example, one of your columns is not nullable in the model but it is nullable in the database and the actual value is null which confuses the materialization? In other words, try to narrow the issue to a specific row in the database.

It looks like that was it. One of the rows had null for one of composite key columns.



来源:https://stackoverflow.com/questions/33742762/code-first-entity-accessible-in-tests-but-not-set-to-an-instance-of-an-object

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