Sequence contains no matching element - EntityFramework

27,527

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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")]

Solution 5

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:

Share:
27,527
choopau
Author by

choopau

Updated on October 20, 2020

Comments

  • choopau
    choopau over 3 years

    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.