New Posts New Posts RSS Feed: intermittent Error while running a query and updates at the same time.
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

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

 Post Reply Post Reply
Author
BringerOD View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 27-Aug-2010
Location: USA
Posts: 35
Post Options Post Options   Quote BringerOD Quote  Post ReplyReply Direct Link To This Post Topic: intermittent Error while running a query and updates at the same time.
    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
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1278
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
BringerOD View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 27-Aug-2010
Location: USA
Posts: 35
Post Options Post Options   Quote BringerOD Quote  Post ReplyReply Direct Link To This Post 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);

}

});

Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1278
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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? 

Back to Top
BringerOD View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 27-Aug-2010
Location: USA
Posts: 35
Post Options Post Options   Quote BringerOD Quote  Post ReplyReply Direct Link To This Post Posted: 10-Sep-2010 at 5:38pm

Agreed.  That was the issue.  Thanks for response.

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down