"Invalid read attempt when there is no data" - an exception occurs "sometimes" in the Entity Framework

Sometimes I get the above error while reading. An exception is thrown from the ASP.NET SqlDataReader whenever you try to read data before calling the Read () method. Since EF does all this internally, I wonder what else might cause this error. could it be network (or) db connectivity?

thanks

Additional information about the bounty (GenericTypeTea):

I have the same error after upgrading to EF Code First RC (4.1):

"Invalid read attempt 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 structure of the table is as follows:

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

The table is autonomous and has no relationship. 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 fine in CTP5. I assume this is an RC bug, but any ideas on how to fix it will be appreciated.

+7
sql-server entity-framework
source share
5 answers

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

+1
source share

Reusing contexts? I assume this is the result of something you do in GetContext

If GetContext() provides an outdated context in which the DataReader is closed / damaged, I could see that this happened.

0
source share

I can not reproduce your problem on EF4.1 RC1.

POCO:

 public class Preference { public string UserName { get; set; } public string PrefName { get; set; } public string Value { get; set; } } 

Context:

 public class PreferenceContext : DbContext { public DbSet<Preference> Preferences {get;set;} public PreferenceContext() : base("Data Source=localhost;Initial Catalog=_so_question_ef41_rc;Integrated Security=SSPI;") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { ConfigurePreference(modelBuilder); base.OnModelCreating(modelBuilder); } private void ConfigurePreference(DbModelBuilder builder) { builder.Entity<Preference>().HasKey(x => new { x.UserName, x.PrefName }); builder.Entity<Preference>().ToTable("RP_Preference"); } } 

My little Console App:

 class Program { static void Main(string[] args) { string userName = "Anon"; for (int i = 0; i < 10000; i++) { var p = GetPreference(userName); } } private static string GetPreference(string userName) { using (var context = new PreferenceContext()) { 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; } } } 

I make 10,000 readings and no errors. You will need to send a more complete code to continue.

0
source share

Increase CommandTimeout in context.

0
source share

I had the same problem with EF4. In my case, I (tried) returned a list of objects inside the {} section. This is the same thing you do in your question:

 return entity == null ? null : entity.Value; 

} // finish using

I moved the return after} and it worked.

I think I had a problem because the code was in a function that already requested the database in another block, I suspect the table was locked, but did not report an error, ending the use block before the return released the database lock data.

Steve

0
source share

All Articles