Effort (C#) - 'Sequence contains no matching element' when seeding data to any of my entities


Question

I am trying to build some unit tests for a few web service calls in my project. I am using Entity Framework 6.1.3 in my project to retrieve the data at the service layer (with a code-first approach). I did some research on how to Mock a DatabaseContext object and I found out that Effort can do this.

I followed the documentation when implementing Effort in my unit tests but when I try to seed data to my entities I get a "System.InvalidOperationException: 'Sequence contains no matching element'" exception. I am not entirely sure why this is happening. Any help will be much appreciated. Thanks!

Here is how the code looks like....

  //THE CALLER -- Doing what Effort suggests to do.

    var dbConnection = DbConnectionFactory.CreateTransient();
        FakeDbContext = new FakeDbContext(dbConnection);
        DatabaseSeed.AddTestData(FakeDbContext);

Here is where I seed data to the entities...

public static void AddTestData(IFakeDbContext context)
    {
        //**************** IT CRASHES HERE *********************
        context.FakeEntity1.AddOrUpdate(new FakeEntity1 
        {
           Name1 = "TestingName1",
           LastName1 = "TestingLastName1"

        });

        context.SaveChanges();
    }

Here is my Entity Model...

[Table("rpt.FakeEntity1")]
public partial class FakeEntity1 : IFakeEntity1 
{
    [Key]
    [Column(Order = 0)]
    [StringLength(20)]
    public string Name1 { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(20)]
    public string LastName1 { get; set; }

}

Here is my DatabaseContext...

public partial class FakeDbContext: IFakeDbContext
{

    public FakeDbContext(DbConnection dbConnection) 
                          : base(dbConnection, true)
    {
        //Effort requires this.
    }

}

public interface IFakeDbContext :IDisposable
{

    DbSet<FakeEntity1> FakeEntity1 {get; set;}

}

I also have another class FakeDbContext.Base that overrides OnModelCreating.

Here is the Stack Trace:

StackTrace

Accepted Answer

The stack trace shows that the exception is thrown when the data provider is looking for a "store type from name". This indicates that you're using a data type that Effort doesn't support. This is confirmed by an issue in the Effort project.


Popular Answer

hope this helps.

Many answers says you have to add FirstorDefault() in your EF statements but this error also occurs in the incorrect properties using data annotations.

When using Data Annotations, be careful with the properties that you add.

Example below, I missed the TypeName = "string", which should have been nvarchar as it is the data type in the database.

Also, take note of the Order = n,(starts with 0)

Encountered this while updating a past colleagues source code.

[Display(Name = "Name")]
[MaxLength(4000)]
[Column("Name", Order = 14, TypeName = "string")]
public string Name { get; set; }

then I corrected my code with

[Display(Name = "Name")]
[MaxLength(4000)]
[Column("Name", Order = 15, TypeName = "nvarchar")]
public string Name { get; set; }

:)





Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why