查询使用有效SQL完成后,EntityFramework中的System.InvalidOperationException

时间:2015-12-02 02:04:57

标签: c# entity-framework

我一直在寻找这个问题的答案,我已经完全陷入了困境。我假设它只是因为我不是在寻找合适的东西,因为我确信解决方案很简单,这只是我做错了。

如果在此之前已经回答过,请将我链接到该主题。

出于某种原因,当我尝试使用Entity Framework从数据库获取数据时,如下面的代码,没有任何反应。当我逐步完成代码时,我发现它已经到了从数据库中获取数据的行,然后只是跳出了函数,没有错误,没有,应用程序只是继续运行,但我不知道得到我的数据。

private void CreateKitWindow_Loaded(object sender, RoutedEventArgs e)
    {
        string kitID = "";
        using (MyContext foo = new MyContext())
        {
            foo.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
            LaminationKit theKit = foo.Lamination_Kit.OrderByDescending(x => x.KitNumber).First();
            kitID = theKit.KitNumber;
        }
        kitNumber.Content = kitID;
    }

我在SO上发现了数据库日志行,并添加了它并检查了结果。

Opened connection at 2/12/2015 2:26:22 p.m. +13:00

SELECT TOP (1) 
[Extent1].[UID] AS [UID], 
[Extent1].[KitNumber] AS [KitNumber], 
[Extent1].[dateCreated] AS [dateCreated], 
[Extent1].[lastTransaction] AS [lastTransaction], 
[Extent1].[daysLeft] AS [daysLeft], 
[Extent1].[hoursLeft] AS [hoursLeft], 
[Extent1].[minsLeft] AS [minsLeft], 
[Extent1].[jobNo] AS [jobNo], 
[Extent1].[assemblyNo] AS [assemblyNo], 
[Extent1].[storageLocation] AS [storageLocation], 
[Extent1].[jobName] AS [jobName], 
[Extent1].[jobDescription] AS [jobDescription], 
[Extent1].[mouldNumber] AS [mouldNumber], 
[Extent1].[targetJob] AS [targetJob], 
[Extent1].[targetAssembly] AS [targetAssembly], 
[Extent1].[targetLine] AS [targetLine], 
[Extent1].[dateFrozen] AS [dateFrozen], 
[Extent1].[dateThawed] AS [dateThawed], 
[Extent1].[cookDate] AS [cookDate]
FROM [dbo].[LaminationKit] AS [Extent1]
ORDER BY [Extent1].[KitNumber] DESC


-- Executing at 2/12/2015 2:26:22 p.m. +13:00

-- Completed in 5 ms with result: SqlDataReader



Exception thrown: 'System.InvalidOperationException' in EntityFramework.dll
Closed connection at 2/12/2015 2:26:22 p.m. +13:00

谁能告诉我这里发生了什么?查询有效并根据此成功完成,但随后抛出异常。 我将这个确切的查询粘贴到SQL服务器管理工​​作室中,它运行正常并且完全符合我的要求。

如果有帮助(删除了额外的东西),这是上下文

class MyContext : DbContext
{
    public MyContext() : base("Password=xxxxxxx;Persist Security Info=True;User ID=xxxxxxxx;Data Source=ssnzsql02;Packet Size=4096;Initial Catalog=SS_Outlife_E10") 
    {
    }

    public DbSet<LaminationKit> Lamination_Kit { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }


}

另一件事是我可以使用更长的

来获取数据
var foo = from item in context.table
          where item.this == something
          Orderby item.that
          select item

 foreach(var t in foo)
 {
        //loop through and do stuff
 }

方法很好,但我不想在每种情况下都这样做。 问题不仅在于此表,它发生在数据库中的许多不同表中。

感谢先进的任何帮助:)

1 个答案:

答案 0 :(得分:1)

谢谢cFrozenDeath和FizzBu​​zz,我不知道为什么我不想在第一时间尝试这个。 (也许是因为在运行时没有发生实际的崩溃或中断)

我扔了一个try / catch,看看它是否能抓住它,我收到了这条消息

The 'dateFrozen' property on 'LaminationKit' could not be set to a 'null' value. You must set this property to a non-null value of type 'System.DateTime'.

事实证明它超级简单,我只是确定,我需要确保我将我的数据类型设置为可为空的表字段允许空值 #EpicFacepalm

改变

public DateTime dateFrozen { get; set; }
to
public Nullable<DateTime> dateFrozen { get; set; }

解决了它。

现在要完成所有事情并修复所有其他事项。