简体   繁体   English

努力(C#)-将数据​​播种到我的任何实体时,“序列不包含匹配元素”

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

I am trying to build some unit tests for a few web service calls in my project. 我正在尝试为我的项目中的几个Web服务调用构建一些单元测试。 I am using Entity Framework 6.1.3 in my project to retrieve the data at the service layer (with a code-first approach). 我在项目中使用Entity Framework 6.1.3检索服务层的数据(采用代码优先方法)。 I did some research on how to Mock a DatabaseContext object and I found out that Effort can do this. 我对如何模拟DatabaseContext对象进行了一些研究,发现Effort可以做到这一点。

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. 在单元测试中实施“工作量”时,我遵循了文档,但是当我尝试将数据播种到实体时,出现“ System.InvalidOperationException:'序列不包含匹配元素'”异常。 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... 这是我的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. 我还有另一个类FakeDbContext.Base,它重写OnModelCreating。

Here is the Stack Trace: 这是堆栈跟踪:

StackTrace 堆栈跟踪

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. 这表明您使用的是Effort不支持的数据类型。 This is confirmed by an issue in the Effort project . 努力项目中的一个问题已证实了这一点。

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. 许多答案表明您必须在EF语句中添加FirstorDefault() ,但使用数据注释的不正确属性中也会发生此错误。

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. 在下面的示例中,我错过了TypeName = "string" ,它应该是nvarchar因为它是数据库中的数据类型。

Also, take note of the Order = n ,(starts with 0) 另外,请注意Order = n ,(从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; }

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM