简体   繁体   中英

System.InvalidOperationException in EntityFramework after query completed with valid SQL

I have been searching for an answer to this for a couple of days now and I'm completely stuck. I am assuming its just that I am not looking for the right thing as I'm sure the solution is simple and it is just something I have done wrong.

For some reason when I try to get data from the database using Entity Framework like this code below, nothing happens. When I stepped through the code I found that it was getting to the line that gets the data from the database and then just jumped out of the function, no errors, nothing, the application just carries on running, but I don't get my data.

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;
}

I found out about the database log line here on Stack Overflow and added it and inspected the results.

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

Can anyone tell me what is happening here? The query is valid and according to this, completed successfully, but then throws an exception. I pasted that exact query into SQL server management studio and it runs fine and does exactly what I want.

Here is the context if that helps (extra stuff removed)

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>();
    }


}

The other thing is that I can get data using the longer

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
 }

Method just fine, but I don't want to do this in every case. The issue is not just on this table, its happened on lots of different tables in the database.

Thank you cFrozenDeath and FizzBuzz, i don't know why i didn't think to try this in the first place. (maybe because no actual crash or break was happening during run time)

I threw a try / catch round it to see if it would catch it and i get this message

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'.

So turns out it was super simple and i just derped, i need to make sure that i set my data types to nullable for table fields that allow null values #EpicFacepalm

changing

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

solved it.

Now to go through everything and fix all the others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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