Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Invalid attempt to read when no data is present' - exception happens "sometimes" in Entity Framework

I get the above error sometimes during the read. The exception originates from ASP.NET SqlDataReader whenever you try to read data before calling the Read() method. Since EF does all these internally, I am wondering what else can cause this error. could it be network (or) db connectivity?

thanks

Additional Bounty Info (GenericTypeTea):

I've got the same error after upgrading to EF Code First RC (4.1):

"Invalid attempt to read when no data is present"

This is the code in question:

using (var context = GetContext())
{
    var query = from item in context.Preferences
                where item.UserName == userName
                where item.PrefName == "TreeState"
                select item;

    // Error on this line
    Preference entity = query.FirstOrDefault();
    return entity == null ? null : entity.Value;
}

The table structure is as follows:

Preference
{
    Username [varchar(50)]
    PrefName [varchar(50)]
    Value [varchar(max)] Nullable
}

The table is standalone and has no relationships. This is the DbModelBuilder code:

private void ConfigurePreference(DbModelBuilder builder)
{
    builder.Entity<Preference>().HasKey(x => new { x.UserName, x.PrefName });
    builder.Entity<Preference>().ToTable("RP_Preference");
}

Exactly the same code works perfectly in CTP5. I'm guessing this is an RC bug, but any ideas of how to fix it would be appreciated.

like image 366
Saravanan Avatar asked Apr 02 '10 17:04

Saravanan


1 Answers

This error occurs when there is a large amount of data in the RC release. The difference between the RC and CTP5 is that you need to specify the [MaxLength] property that contains a large amount of data.

like image 138
djdd87 Avatar answered Nov 02 '22 02:11

djdd87