1
Vote

System.InvalidOperationException was unhandled by user code HResult=-2146233079 Message=Sequence contains no matching element

description

Attempting to add test data like this using EF 6.1.3:
using System;
using System.Data.Entity.Migrations;
using Cssd.IT.PortalIntegration.POCO.SISMid.BO;

namespace Cssd.IT.PortalIntegration.DataAccessTest
{
    public class SISMidDataAccessTestData
    {
        public static void AddTestData(ISISMidDbContext context)
        {
            context.LookupGrade.AddOrUpdate(new LookupGrade { GradeCode = -1, Grade = "PK4" });
            context.LookupGrade.AddOrUpdate(new LookupGrade { GradeCode = 0, Grade = "00" });
            context.LookupGrade.AddOrUpdate(new LookupGrade { GradeCode = 1, Grade = "01" });
            context.LookupGrade.AddOrUpdate(new LookupGrade { GradeCode = 6, Grade = "06" });
            context.LookupGrade.AddOrUpdate(new LookupGrade { GradeCode = 9, Grade = "09" });
            context.LookupGrade.AddOrUpdate(new LookupGrade { GradeCode = 12, Grade = "12" });
        }
    }
}
Here is the LookupGrade class:
namespace Cssd.IT.PortalIntegration.POCO.SISMid.BO
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;
    using Cssd.IT.PortalIntegration.POCO.School.BO;

    public partial class LookupGrade : IImportBusinessKey
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int GradeCode { get; set; }

        [Required]
        [StringLength(3)]
        public string Grade { get; set; }

        [Column(TypeName = "smalldatetime")]
        public DateTime? CC_SISMIDRefreshDate { get; set; }

        public string ImportBusinessKey
        {
            get { return this.GradeCode.ToString(); }
        }
    }
}
This is my database table, where GradeCode is an integer non-identity primary key:
/****** Script for SelectTopNRows command from SSMS  ******/
SELECT TOP 1000 [GradeCode]
      ,[Grade]
      ,[CC_SISMIDRefreshDate]
  FROM [CC_SISMID].[dbo].[LookupGrade]
When the first AddOrUpdate executes, receiving this exception:
System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=Sequence contains no matching element
  Source=System.Core
  StackTrace:
       at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
       at System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest, String name)
       at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.ConfigureColumn(EdmProperty column, EntityType table, DbProviderManifest providerManifest)
       at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(EdmProperty column, EntityType table, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration)
       at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.<>c__DisplayClass4.<Configure>b__3(Tuple`2 pm)
       at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
       at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration)
       at System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.ConfigurePropertyMappings(IList`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride)
       at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigurePropertyMappings(DbDatabaseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest, Boolean allowOverride)
       at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
       at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, ICollection`1 entitySets, DbProviderManifest providerManifest)
       at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
       at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
       at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
       at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
       at System.Data.Entity.Migrations.DbSetMigrationsExtensions.GetKeyProperties[TEntity](Type entityType, InternalSet`1 internalSet)
       at System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate[TEntity](IDbSet`1 set, TEntity[] entities)
       at Cssd.IT.PortalIntegration.DataAccessTest.SISMidDataAccessTestData.AddTestData(ISISMidDbContext context) in c:\DevProjects\IT_Portal\Source\PortalIntegration\Cssd.IT.PortalIntegration.DataAccessTest\SISMidDataAccessTestData.cs:line 12
       at Cssd.IT.PortalIntegration.DataAccessTest.SISMidDataAccessTest.Setup() in c:\DevProjects\IT_Portal\Source\PortalIntegration\Cssd.IT.PortalIntegration.DataAccessTest\SISMidDataAccessTest.cs:line 27
  InnerException: 
Does anyone have any suggestions? The above error occurs only when attempting to test with Effort, in code-first entity framework mode. When the unit test is run against SQL Server, or if we run in database-first entity framework mode, it works fine. TIA.

comments

RobertGTaylor1 wrote Nov 6, 2015 at 1:46 PM

One thing to do is to look at the inner exception and see if there are any validation errors reported. This helped me when I failed to spot a 'required' field.