命令执行时实体框架常规错误

发布于 2024-10-25 04:44:27 字数 3287 浏览 2 评论 0原文

我正在开发一个使用实体框架进行数据访问的 WPF 应用程序。作为设计模式,我使用“MVVM”进行层组织,使用“Repository”和“UnitOfWork”进行数据层组织。

通用存储库类:



    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_value);

有时这里会抛出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 次迭代周期。但没有什么导致这个异常。

如果需要,我可以发布任何其他信息。

[2011年3月23日]

其他信息:

  • Entity Framework 4.0
  • MSSQL Server 2008
  • 每当查询发生时都可能引发此异常。它可以是对小表(<200 行)或大表(>500k 行)的 l2e 查询。另外这个异常也可能是由 Function Import 调用引起的
  • ,当抛出这个异常时,

    {"Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding."}

它会立即抛出,但连接超时设置为 30 秒! (我认为这是“关键特征”)

  • 数据库服务器位于局域网中,数据库上没有大流量。
  • 正如我发现的,无论使用哪种查询或表,此异常都可能随时发生。
  • 我不使用交易。即使我只使用选择查询也会发生此错误。

我认为问题是由将 WPF 与 EF 一起使用引起的,因为我的“EF 部分”在控制台应用程序中工作正常。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独闯女儿国 2024-11-01 04:44:27

ObjectContext 有两个超时参数:

int x = myObjectContext.Connection.ConnectionTimeout;

查看

int? x = myObjectContext.CommandTimeout;

异常和调用堆栈,我认为 ConnectionTimeout 不是问题(这是尝试建立连接的最长时间)从客户端到 SQL Server)。您可以尝试将 CommandTimeout 设置为某个较高的值(这是查询和使用 SaveChanges 提交的超时;值 null 表示默认值将采用底层提供商的信息)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文