Enity Framework regular error while command executing
I am developing a WPF Applicaton using Entity Framework for data access. As design pattern i use "MVVM" for tier organization, and "Repository" with "UnitOfWork" for data layer organization.
Generic Repository class:
public class EFRepository : IRepository where T : class
{
public IUnitOfWork UnitOfWork { get; set; }
private IObjectSet _objectset;
private IObjectSet ObjectSet
{
get
{
if (_objectset == null)
{
_objectset = UnitOfWork.Context.CreateObjectSet();
}
return _objectset;
}
}
public virtual IQueryable All()
{
return ObjectSet.AsQueryable();
}
...
Unit of work interface:
public interface IUnitOfWork: IDisposable
{
ObjectContext Context { get; set; }
void Save();
bool LazyLoadingEnabled { get; set; }
bool ProxyCreationEnabled { get; set; }
string ConnectionString { get; set; }
}
I build all L2E queries in model layer, like:
this.repository1.All().First(i => i.Field1 == some_value);
Sometimes here is thrown an EntityCommandExecutionException. It happens sometimes, but not regular. There is no rule to make this exception occur.
Exception Detail:
EntityCommandExecutionException:
{"An error occurred while executing the command definition. See the inner exception for details."}
InnerException:
{"Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."}
StackTrace:
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 开发者_JS百科forMergeOption)
at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator()
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
at System.Data.Objects.ELinq.ObjectQueryProvider.b__0[TResult](IEnumerable`1 sequence)
at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.First[TSource](IQueryable`1 source, Expression`1 predicate)
Please, help me to find out the problem, i really don`t know what to do now =(
p.s.:
I tried to provoke this error building and executing L2E queries in simple Console Application. I tried single L2E queries and through 1000-iterations cycles. But nothing caused this exception.I can post any additional information if needed.
[23.03.2011]
Additional Info:
- Entity Framework 4.0
- MSSQL Server 2008
- this exception can be thrown any time the query take place. It can be l2e query to small table (<200 rows) or large (>500k rows). Also this exception can be caused by Function Import call
- when this exception is thrown,
{"Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."}
it is thrown immediately, but the connection timeout is set to 30 seconds! (i think that is "key feature")
- DB Server is situated in LAN, and there is no heavy traffic on DB.
- as i have found out, this exception can occur any time no matter what kind of queries or tables are used.
- I dont use transaction. This error occur even if i use only select queries.
I think the problem caused by using WPF with EF, because my "EF part" works fine in Console Application.
The ObjectContext
has two timeout parameters:
int x = myObjectContext.Connection.ConnectionTimeout;
and
int? x = myObjectContext.CommandTimeout;
Looking at the exception and callstack I don't think that the ConnectionTimeout
is the problem (that's the maximum time to try to establish a connection from the client to SQL Server). You could try to set the CommandTimeout
to some high value (that's the timeout for queries and commits with SaveChanges
; the value null
means that the default of the underlying provider will be taken).
精彩评论