Sequence contains no matching element - EntityFramework

天大地大妈咪最大 提交于 2019-11-28 10:45:06

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.

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.

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

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

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:

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…

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!

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.

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

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