枚举没有导入函数的结果

时间:2016-02-13 17:16:35

标签: c# entity-framework visual-studio-2015 entity-framework-6 dbcontext

我尝试像这样测试我导入的函数:

[TestMethod]
public void TestCustomerById()
{
    using (var ctx = new AWLT.AWLT())
    {
        var customer = ctx.GetCustomerById(1);
        // Assert.AreNotEqual(0, customer.Count());
        Assert.AreNotEqual(0,
            ((IObjectContextAdapter)ctx).ObjectContext
            .ObjectStateManager
            .GetObjectStateEntries(System.Data.Entity.EntityState.Unchanged).Count());
    }
}

当我调试测试并尝试快速观看customer时,我得到:

  

枚举没有产生结果

虽然当我运行程序时,它会返回一个客户!!

1 个答案:

答案 0 :(得分:0)

你应该调用.ToList()强制枚举。

[TestMethod]
public void TestCustomerById()
{
    using (var ctx = new AWLT.AWLT())
    {
        var customer = ctx.GetCustomerById(1).ToList();
        // Assert.AreNotEqual(0, customer.Count());
        Assert.AreNotEqual(0,
            ((IObjectContextAdapter)ctx).ObjectContext
            .ObjectStateManager
            .GetObjectStateEntries(System.Data.Entity.EntityState.Unchanged).Count());
    }
}