Sequence contains no matching element - EntityFramework

后端 未结 10 1646
盖世英雄少女心
盖世英雄少女心 2020-12-17 08:43

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 creat

相关标签:
10条回答
  • 2020-12-17 09:21

    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);
    
    0 讨论(0)
  • 2020-12-17 09:25

    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!

    0 讨论(0)
  • 2020-12-17 09:25

    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.

    0 讨论(0)
  • 2020-12-17 09:28

    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;
    }
    
    0 讨论(0)
  • 2020-12-17 09:29

    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…

    0 讨论(0)
  • 2020-12-17 09:35

    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.

    0 讨论(0)
提交回复
热议问题