Sequence contains no matching element - EntityFramework
Asked Answered
A

11

25

I'm using EF 6.1.0 and was creating a WCF Service.

First I created a Class Library containing my entities, Mappers and Context for initializing EF. I've also created a class containing an instantiation of the context and have this code:

public IQueryable<[Entity]> GetAll()
{
    return context.[Entity].AsQueryable();
}

On the otherhand, I've created a WCF Service on the same project and Calls the function GetAll() in the.svc file like this:

public List<[Entity]> GetList()
{
    [iObject] repository = new [Object](new Context());
    return repository.GetAll().ToList();
}

The project is building. I even check the cconfiguration and it is in the right DB. However, database and tables which is supposedly created doesn't exist and an error "Sequence contains no matching element" shows up in the return.

If this one is confusing can you point me some link of WCF Services using Code First Entity Framework.

Adelbert answered 10/4, 2014 at 8:32 Comment(0)
M
64

I had a bug where I got a System.InvalidOperationException with the message Sequence contains no matching element because I had an incorrect string value being passed to .HasColumnType() in my table configuration.

Minor answered 2/10, 2014 at 8:15 Comment(8)
Oh My God. Could we have a worse error being thrown for this? Thanks for posting, saved me good bit of time.Dichromatic
I had the error because of a wrong attribute in model: [Column(TypeName = "decimal(9,4)")]Suellen
@Suellen same pinch. I was getting this error when I mentioned attribute [Column("ToDoItem", TypeName = "nvarchar(4000)")] on one of the properties. I was following code first approach where DB was not present beforehand. It got corrected when I changed nvarchar(4000) to nvarchar. Surprisingly when we mention nvarchar then EF creates the column with nvarchar(4000) data type only. thanks to EF team for raising this mysterious error.Ng
I just figured out length of varchar or nvarchar data type requires to be mentioned as a separate attribute called StringLength. More interestingly [Column("ToDoItem", TypeName = "NVARCHAR(MAX)")] works. For MAX nvarchar length you don't need to mention the stringLength attribute separately.Ng
I had set it to: .HasColumnType("image") for mysql and it was throwing an error. thanks a bunch.Bord
Yes thanks a lot, in my case I was using the "[Column(TypeName = "XXX")]" attribute, but same crazy exception. Thanks for the good catch!Distemper
Arghhhhhhhh what kind of exception! Thanks a lot never would have guessed and did a commit too long ago, lesson learnedAurea
That moment you find this answer and it saves your day, you want to Upvote it and see you already upvoted it in the past... facepalmReedy
T
11

I just experienced the same problem. The Code First from database wizard generated one of the columns as [Column(TypeName = "date")].

Calling .saveChanges() resulted in the Sequence contains no elements.

After changing the defined column to [DataType(DataType.DateTime)] it worked as expected.

Tiptoe answered 20/4, 2016 at 13:13 Comment(2)
I faced the exact same issue and this helped. That is a TERRIBLE exception. Absolutely not useful in the least!Lawannalawbreaker
<Column(TypeName:="datetime")> and <Column(TypeName:="datetime2")> work as expected, just FYI.Virescence
Y
7

I was also getting same error while adding migration or updating database.

The reason was I was using wrong column type

[Column("ImageType", TypeName = "varchar(20)")]
public string ImageType
{
     get;
     set;
}

But when I removed 20 from varchar it started working for me

[Column("ImageType", TypeName = "varchar")]
public string ImageType
{
    get;
    set;
}
Yalu answered 13/7, 2017 at 19:11 Comment(0)
L
3

For me this message was because of misunderstanding for TypeName in Column attribute.

Column Attribute: [Column (string name, Properties:[Order = int],[TypeName = string])

name: Name of a column in a db table.
Order: Order of a column, starting with zero index. (Optional)
TypeName: Data type of a column. (Optional)

This TypeName must be only name of the type and must not include precision or scale or length and any other thing. For example following will cause an error

[Column(TypeName = "nvarchar(600)")]

while below one will work fine however you might want to have specific size of the column and for that one way is to use fluent API

[Column(TypeName = "nvarchar")]

Leoraleos answered 10/6, 2018 at 10:51 Comment(0)
F
2

Entity Framework throws this exception if the column type is invalid. For example:

// This will throw an error. There is no such type name.
[Column(TypeName = "Invalid")]
public string Column1 { get; set; }

// Works.
[Column(TypeName = "varchar")]
public string Column1 { get; set; }

See these examples:

Firstborn answered 20/7, 2017 at 18:35 Comment(0)
C
1

Yup. Caught me as well because in a moment of distraction I put the literal long in there for a bigint column i.e. HasColumnType("long")- can you believe it?! What a clown!

Generators generally create your EF classes properly, but if you're not able to or don't want to use one then you could use a static class with some static string fields on them so that you can intellisense it quite nicely:

public static class DatabaseColumnTypes
{
    /// <summary>
    /// Use this for 'boolean' values.
    /// </summary>
    public static string BitColumn = "bit";

    /// <summary>
    /// Use this for 'byte' values.
    /// </summary>
    public static string TinyIntColumn = "tinyint";

    /// <summary>
    /// Use this for 'long' values.
    /// </summary>
    public static string BigIntColumn = "bigint";

    /// <summary>
    /// Use this for 'string' values.
    /// </summary>
    public static string VarcharColumn = "varchar";

    // etc
}

Now you can do HasColumnType( DatabaseColumnTypes.BigIntColumn )

I know, I know, this is lazy, I should remember these types but every so often I find myself on Stack Overflow looking it up and this just saves time…

Cassimere answered 26/8, 2017 at 22:25 Comment(0)
B
0

I got the same error message when I had renamed my Member model to Student and I had a navigation property in some other class as:

public IList<Student> Members { get; set; }

I changed that to:

public IList<Student> Students { get; set; }

and the problem was resolved!

Betimes answered 11/4, 2017 at 10:23 Comment(0)
U
0

I got this error while defining Table-Per-Type Inheritance with Fluent API (as a class deriving from EntityTypeConfiguration<T>) and mistakenly redefined field that was already defined in base class configuration (and both were exactly the same). I was using EntityFramework 6.2.0 nuget package.

Utah answered 25/6, 2018 at 9:8 Comment(0)
N
0

I got caught with this one because I referenced the reference property instead of the Id property.

        var t = modelBuilder.Entity<CyDocument>();
        t.HasIndex(b => b.DocTypeId);
Nonconformist answered 20/10, 2019 at 9:48 Comment(0)
C
0

I was stuck with same exception System.InvalidOperationException with the message Sequence contains no matching element. This was in Entity Framework 6.0.0. When I upgraded to Entity Framework 6.4.4 I got exception with clear message Additional information: The store type 'VARCHAR2(10)' could not be found in the OracleEFProvider provider manifest

[Column("SCOPE_ID", TypeName = "varchar(10)")]
public string ScopeId
{
     get;
     set;
}

I changed my coded as below, by removing TypeName

[Column("SCOPE_ID"]
public string ScopeId
{
    get;
    set;
}
Copalite answered 16/9, 2022 at 9:16 Comment(0)
W
-1

For me it helped, when switched class project to Set as startup project

Wireless answered 6/1, 2017 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.