Entity framework Enity Framework在执行命令时发生常规错误
我正在开发一个WPF应用程序,使用实体框架进行数据访问。作为设计模式,我使用“MVVM”作为分层组织,使用带有“UnitOfWork”的“Repository”作为数据层组织。 通用存储库类:Entity framework Enity Framework在执行命令时发生常规错误,entity-framework,linq-to-entities,Entity Framework,Linq To Entities,我正在开发一个WPF应用程序,使用实体框架进行数据访问。作为设计模式,我使用“MVVM”作为分层组织,使用带有“UnitOfWork”的“Repository”作为数据层组织。 通用存储库类: public class EFRepository : IRepository where T : class { public IUnitOfWork UnitOfWork { get; set; } private IObjectSet _objectset;
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();
}
...
工作单位界面:
public interface IUnitOfWork: IDisposable
{
ObjectContext Context { get; set; }
void Save();
bool LazyLoadingEnabled { get; set; }
bool ProxyCreationEnabled { get; set; }
string ConnectionString { get; set; }
}
我在模型层构建所有L2E查询,如:
this.repository1.All().First(i=>i.Field1==some_值)代码>
有时这里会抛出EntityCommandExecutionException。有时会发生,但不是经常发生。没有规则使此异常发生。
例外情况详情:
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 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)
请帮我找出问题所在,我真的不知道现在该怎么办=(
附笔。:
我试图在简单控制台应用程序中创建和执行L2E查询时引发此错误。我尝试了单个L2E查询,并经历了1000次迭代周期。但没有任何结果导致此异常。
如果需要,我可以发布任何附加信息
[23.03.2011]
其他信息:
- 实体框架4.0
- MSSQL服务器2008
- 此异常可以在任何时候进行查询时引发。它可以是对小表(500k行)的l2e查询。此异常也可能是由函数导入调用引起的
- 当抛出此异常时
它会立即抛出,但连接超时设置为30秒!(我认为这是“关键特性”)
- 数据库服务器位于局域网内,数据库上没有大流量
- 正如我所发现的,无论使用何种查询或表,这种异常随时都可能发生
- 我不使用事务。即使我只使用选择查询,也会发生此错误
我认为将WPF与EF一起使用会导致问题,因为我的“EF部分”在控制台应用程序中运行良好。ObjectContext有两个超时参数:
int x = myObjectContext.Connection.ConnectionTimeout;
及
查看异常和调用堆栈,我认为ConnectionTimeout
不是问题所在(这是尝试建立从客户端到SQL Server的连接的最长时间)。您可以尝试将CommandTimeout
设置为某个高值(这是使用SaveChanges
进行查询和提交的超时;值null
表示将采用基础提供程序的默认值)。可能会引起兴趣的其他信息:EF version.Database(SQL Server?哪个版本?)。您正在查询的表是否很大?数据库上是否存在大量流量?数据库是否远程,或与应用程序在同一系统上?生产和测试环境中是否存在此问题?是否所有类型的查询都会出现异常,或者仅特定的查询或表受到影响?可能是最重要的信息:是否存在长时间运行的trAnActions?您对事务使用什么隔离级别?具有长时间运行事务的单个事务作用域可以从读取中锁定记录,并且不属于同一事务的所有选择和请求锁定的数据都将超时。谢谢您,Slauma,我将尝试此操作。
int x = myObjectContext.Connection.ConnectionTimeout;
int? x = myObjectContext.CommandTimeout;