LINQ: The specified video is not valid

I have a RANDOM error (1 of 100 page loads) in the following line of code:

topic = TopicsContext.GetCurrentDataContext().tTopics.Where(t => t.ContentId == contentId).SingleOrDefault();

Both the ContentId property and the conntentId local variables are long.

Most importantly, the error occurs randomly, in most cases it works fine.

Thanks in advance for your ideas.

Here is the complete exception information:

 Error Message: Specified cast is not valid.
 Error Source: System.Data.Linq
 Error Stack Trace: 
   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
   at Topics.BusinessLogic.Models.Services.TopicService.GetTopic(String title) in C:\rabota\topics\source\trunk\Topics.BusinessLogic\Models\Services\TopicService.cs:line 65
+3
source share
3 answers

, LINQ-to-SQL . , , , , . , LINQ to SQL .

+3

, SingleOrDefault(). , , (t.contentId) ( , ?), contentId , . " " NULL, , (=) null, null .

:

long? topic = ....

"Single()" catch catch, , .

, RANDOM .

0

I got a similar error when I had a column of type varchar (1) that was mapped to char and not to row. This worked if the column value was not null when casting null to char, which threw the exception "specified cast is invalid."

See http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/ac91e587-6e91-454c-9fa2-bab20b7a258c for details .

0
source

Source: https://habr.com/ru/post/1711916/


All Articles