Print Page | Close Window

intermittent Error while running a query and updates at the same time.

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2140
Printed Date: 10-Jun-2026 at 7:09pm


Topic: intermittent Error while running a query and updates at the same time.
Posted By: BringerOD
Subject: intermittent Error while running a query and updates at the same time.
Date Posted: 09-Sep-2010 at 1:15pm
I get the following error on my async return function.
 
{"The cast to value type 'Guid' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type."}
 
I have a timed event that runs and adds data to our system every 10 minutes.  During this process we get this error intermidently above when querying the table.
 
I am not specifying any special transactions. 
 
This behavior is intermident. 
 
I had nolocks on the view I was pulling from, but I removed them and the behavior still exists.
 
This does not happpen in I am not adding and updating records at the same time.
 
This has to occur. Anyone have a work around?
 
Bryan



Replies:
Posted By: kimj
Date Posted: 10-Sep-2010 at 11:42am
Is the handler for the timed event running on a different thread?  An EntityManager is not thread safe and should not be used on multiple threads concurrently.  In release 6.0.5 we added the AuthorizedThreadId property on the EntityManager to specifically check for this type of thing.  So, I'd first rule out the possibility of multi-threading.


Posted By: BringerOD
Date Posted: 10-Sep-2010 at 11:50am
I figured out a workaround.
 
Not sure exactly why it solved it, but it did.
 
StatusId = g.FirstOrDefault().StatusId ?? Guid.Empty,
 

This line below which detects if the status column is null and replaces it with an empty Guid solve the issue. 

 

The troubling part is there should never be an instance of the statusId being null. 

 

Thanks for your response.   I thought it was something like you mentioned.  That is why I posted the question. 

 

The only thing I can think of is on the SaveChanges of the entities on the import is NOT wrapped in a transaction.  This would leave the StatusId column null for a millisecond.

 
-----------------------------------------------------------------------------------------------------
 

var query2 = query1.Select(g => new

{

g.FirstOrDefault().DisplayOrder,

StatusId = g.FirstOrDefault().StatusId ?? Guid.Empty,

StatusName = g.Key,

TotalLeads = g.Count(),

g.FirstOrDefault().FontColor,

g.FirstOrDefault().BackgroundColor

}).OrderBy(x => x.DisplayOrder);

query2.ExecuteAsync((op) =>

{

StatusGroupDtos.Clear();

foreach (var result in op.Results)

{

Type type = result.GetType();

var newItem = new LeadStatusGroupDto();

newItem.StatusId = (Guid) type.GetProperty("StatusId").GetValue(result, null);

newItem.StatusName = (string) type.GetProperty("StatusName").GetValue(result, null);

newItem.TotalLeads = (int) type.GetProperty("TotalLeads").GetValue(result, null);

newItem.FontColor = (string) type.GetProperty("FontColor").GetValue(result, null);

newItem.BackgroundColor = (string) type.GetProperty("BackgroundColor").GetValue(result, null);

newItem.DisplayOrder = (int?) type.GetProperty("DisplayOrder").GetValue(result, null);

StatusGroupDtos.Add(newItem);

}

});



Posted By: kimj
Date Posted: 10-Sep-2010 at 5:33pm

After seeing your query I don't think this is a multi-threading issue.  I don't know what you're grouping by, but is it possible that the grouping could result in a null status id, depending on the data retrieved or grouping criteria? 



Posted By: BringerOD
Date Posted: 10-Sep-2010 at 5:38pm

Agreed.  That was the issue.  Thanks for response.




Print Page | Close Window