EntityFramework-Effort: System.InvalidOperationException: Sequence contains no matching element

Just to emphasize, codeplex currently doesn’t support adding new threads to issue or discussion board, otherwise I would post this issue there.

This simple test fails on any attempt to work with DbSet.

public async Task TestMethod1()
{
    DbConnection inMemoryConnection =                
        Effort.DbConnectionFactory.CreateTransient();

    using (var dbContext = MyDbContext.CreateDbContext(inMemoryConnection)) 
    {
        // Fails on this line (triggered by Add method):
        dbContext.Users.Add(new User()
        {
            Id = 1                         
        });
        var item = dbContext.User.Find(1);
        Assert.AreEqual(1, item.Id);
    } 
}

Tried Methods like Add, Find, FindAsync, linq queries, etc. Also tried with a couple of even simpler entities. Tried using IDbSet as well. Also played around with the app.config and finally removed any references to Effort from the config in regards to DB connection.

I have no idea what I’m missing… Any suggestions?

Top exception is:

_System.InvalidOperationException: Sequence contains no matching element_

Test Name:  TestMethod1
Test FullName:  ****
Test Source:    **** : line 21
Test Outcome:   Failed
Test Duration:  0:00:00,5738216

Result Message: 
Test method ****.TestMethod1 threw exception: 
System.InvalidOperationException: Sequence contains no matching element
Result 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.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
   at System.Data.Entity.DbSet`1.Add(TEntity entity)
   at ****.<TestMethod1>d__1.MoveNext() in ****:line 29
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Partial of the MyDbContext:

public class MyDbContext : DbContext
{    
    public DbSet<User> Users { get; set; }

    private MyDbContext() : base()
    {
        this.Configuration.LazyLoadingEnabled = false;
    }

    private MyDbContext(DbConnection connection) : base(connection, true)
    {
        this.Configuration.LazyLoadingEnabled = false;
    }

    public static MyDbContext CreateDbContext(DbConnection connection = null)
    {
        if (connection == null)
           return new MyDbContext();

        return new MyDbContext(connection);
    }
}

This is the User Entity.

public class User
{

    [Key]
    public long Id { get; set; }

    [Required]
    [MaxLength(PropertyLengths.UserUsernameMax)]
    [MinLength(PropertyLengths.UserUsernameMin)]
    [Index(IsUnique = true)]
    public string Username { get; set; }

    [Required]
    [MaxLength(PropertyLengths.UserEmailMax)]
    [EmailAddress]
    [Index(IsUnique = true)]
    public string Email { get; set; }

    public DateTime RegistrationTime { get; set; }

    [Required]
    [MaxLength(PropertyLengths.UserPasswordBinaryMax)]
    public byte[] Password { get; set; }

    public bool IsSupaUza { get; set; }

    public virtual ICollection<SomeEntity1> SomeEntities1 { get; set; }

    public virtual ICollection<SomeEntity2> SomeEntities2 { get; set; }

    public virtual ICollection<SomeEntity3> SomeEntities3 { get; set; }

    [MaxLength(PropertyLengths.DefaultMax)]
    public string FirstName { get; set; }

    [MaxLength(PropertyLengths.DefaultMax)]
    public string LastName { get; set; }

    [Column(TypeName = "Date")]
    public DateTime DateOfBirth { get; set; }

    [MaxLength(PropertyLengths.BlobUri)]
    public string MyCoolPic { get; set; }     
}

Unit test project app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />      
    </providers>
  </entityFramework>  
</configuration>

Unit test project packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Effort.EF6" version="1.1.4" targetFramework="net45" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net45" />
  <package id="NMemory" version="1.0.1" targetFramework="net45" />
</packages>

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 30 (1 by maintainers)

Most upvoted comments

I simply removed the explicit “ntext” configuration from my entity type configuration:

Property(x => x.DocumentationLinks)
            .HasColumnType("ntext");

After this, the property was mapped as nvarchar(max) instead (by default) and my problem was resolved, the rest of my model did not have any explicit HasColumnType configurations and Effort worked fine.

workaround link appears to be broken, anyone have a working one?

I’m also using “EntityFramework Reverse POCO Code First Generator” and hitting similar problem. Is there a solution/good-workaround for this?

Same problem with

[Column(TypeName = "varbinary(max)")]

Have elected to ignore that property for unit tests.

// my hack...
if (this.Database.Connection.GetType().Name.StartsWith("Effort", StringComparison.Ordinal))
{
    modelBuilder.Entity<Building>().Ignore(_ => _.Geolocation);
    modelBuilder.Entity<AttachedDocument>().Ignore(_ => _.Contents);
}