选择First()时出现OutOfMemoryException

时间:2018-04-20 10:50:33

标签: c# .net entity-framework-6

编辑:经过大量测试后,我发现问题根本与OutOfMemoryException无关。感谢您的帮助,这促使我寻找其他原因。

此案例已关闭!

我有一个奇怪的问题。我的程序自去年夏天以来一直有效,现在他们将Windows 10更新为1709版(计算机通常不连接)

现在,当我尝试访问数据库时,我收到了System.OutOfMemoryException。

该表包含大约100万行,但由于我使用的是First(),它不应该重要(应该吗?)

有人知道出了什么问题?

编辑: 添加了上下文和POCO的代码。

编辑2: 为context.LaserDate.First();

添加了SQL查询

数据库是MS SQL Server 2014,可通过本地网络访问。

代码如下所示:

internal void IsConnectionOK()
{
    try
    {
        using (var context = new DCLasermarkContext())
        {
            var tmp = context.LaserData.First();
        }
    }
    catch (Exception ex)
    {
        Log.Fatal(ex, "IsConnection Failed!");
        throw;
    }
}

和例外:

2018-04-20 07:29:37.3068 | FATAL | DCMarker.Model.DB.IsConnectionOK | IsConnection Failed! | System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Data.Entity.Core.Common.EntitySql.CqlParser.yyparse()
   at System.Data.Entity.Core.Common.EntitySql.CqlParser.internalParseEntryPoint()
   at System.Data.Entity.Core.Common.EntitySql.CqlParser.Parse(String query)
   at System.Data.Entity.Core.Common.EntitySql.CqlQuery.Parse(String commandText, ParserOptions parserOptions)
   at System.Data.Entity.Core.Common.EntitySql.CqlQuery.CompileCommon[TResult](String commandText, ParserOptions parserOptions, Func`3 compilationFunction)
   at System.Data.Entity.Core.Common.EntitySql.CqlQuery.CompileQueryCommandLambda(String queryCommandText, Perspective perspective, ParserOptions parserOptions, IEnumerable`1 parameters, IEnumerable`1 variables)
   at System.Data.Entity.Core.Objects.EntitySqlQueryState.Parse()
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateInlineQueryOfT(ObjectQuery inlineQuery)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.ObjectQueryMergeAsTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.ObjectQueryMergeAsTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.UnarySequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
   at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()
   at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__0[TResult](IEnumerable`1 sequence)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.First[TSource](IQueryable`1 source)
   at DCMarker.Model.DB.IsConnectionOK()

编辑:

public partial class DCLasermarkContext : DbContext
{
    public DCLasermarkContext()
        : base("name=DCLasermarkContext")
    {
        // Without this line, the compiler will optimize away  System.Data.Entity.SqlServer
        var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);

        // make sure that we don't initialize the database!!
        Database.SetInitializer<DCLasermarkContext>(null);
    }

    public virtual DbSet<Fixture> Fixture { get; set; }
    public virtual DbSet<HistoryData> HistoryData { get; set; }
    public virtual DbSet<LaserData> LaserData { get; set; }
    public virtual DbSet<QuarterCode> QuarterCode { get; set; }
    public virtual DbSet<SerialNumber> SerialNumber { get; set; }
    public virtual DbSet<WeekCode> WeekCode { get; set; }

    protected override System.Data.Entity.Validation.DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, System.Collections.Generic.IDictionary<object, object> items)
    {
        if (entityEntry.Entity is LaserData)
        {
            if (string.IsNullOrWhiteSpace(entityEntry.CurrentValues.GetValue<string>("F1")))
            {
                var list = new List<System.Data.Entity.Validation.DbValidationError>();
                list.Add(new System.Data.Entity.Validation.DbValidationError("F1", GlblRes.ArticleF1_is_required));

                return new System.Data.Entity.Validation.DbEntityValidationResult(entityEntry, list);
            }
        }

        return base.ValidateEntity(entityEntry, items);
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //modelBuilder.Types()

        modelBuilder.Entity<Fixture>()
            .Property(e => e.FixturId)
            .IsUnicode(false);

        modelBuilder.Entity<HistoryData>()
            .Property(e => e.Kant)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<HistoryData>()
            .Property(e => e.DateMark)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<HistoryData>()
            .Property(e => e.DateMarkLong)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<HistoryData>()
            .Property(e => e.DateMarkShort)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<LaserData>()
            .Property(e => e.Kant)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<QuarterCode>()
            .Property(e => e.QYear)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<QuarterCode>()
            .Property(e => e.Q1)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<QuarterCode>()
            .Property(e => e.Q2)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<QuarterCode>()
            .Property(e => e.Q3)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<QuarterCode>()
            .Property(e => e.Q4)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<WeekCode>()
            .Property(e => e.Code)
            .IsFixedLength()
            .IsUnicode(false);
    }
}

[Table("LaserData")]
public partial class LaserData
{
    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string F1 { get; set; }

    [StringLength(1)]
    public string Kant { get; set; }

    [StringLength(100)]
    public string Avdelning { get; set; }

    [StringLength(50)]
    public string F2 { get; set; }

    [StringLength(50)]
    public string F3 { get; set; }

    [StringLength(50)]
    public string F4 { get; set; }

    [StringLength(50)]
    public string F5 { get; set; }

    [StringLength(50)]
    public string F6 { get; set; }

    [StringLength(50)]
    public string F7 { get; set; }

    [StringLength(50)]
    public string F8 { get; set; }

    [StringLength(50)]
    public string F9 { get; set; }

    [StringLength(50)]
    public string F10 { get; set; }

    [StringLength(50)]
    public string BC1 { get; set; }

    [StringLength(50)]
    public string BC2 { get; set; }

    [StringLength(50)]
    public string Template { get; set; }

    [StringLength(50)]
    public string P1 { get; set; }

    [StringLength(50)]
    public string P2 { get; set; }

    [StringLength(50)]
    public string P3 { get; set; }

    [StringLength(50)]
    public string P4 { get; set; }

    [StringLength(50)]
    public string P5 { get; set; }

    [StringLength(50)]
    public string P6 { get; set; }

    [StringLength(50)]
    public string FixtureId { get; set; }

    public bool? ExternTest { get; set; }

    public bool? EnableTO { get; set; }

    [StringLength(50)]
    public string TOnr { get; set; }
}

查询context.LaserData.First()

SELECT TOP (1) 
    [c].[Id] AS [Id], 
    [c].[F1] AS [F1], 
    [c].[Kant] AS [Kant], 
    [c].[Avdelning] AS [Avdelning], 
    [c].[F2] AS [F2], 
    [c].[F3] AS [F3], 
    [c].[F4] AS [F4], 
    [c].[F5] AS [F5], 
    [c].[F6] AS [F6], 
    [c].[F7] AS [F7], 
    [c].[F8] AS [F8], 
    [c].[F9] AS [F9], 
    [c].[F10] AS [F10], 
    [c].[BC1] AS [BC1], 
    [c].[BC2] AS [BC2], 
    [c].[Template] AS [Template], 
    [c].[P1] AS [P1], 
    [c].[P2] AS [P2], 
    [c].[P3] AS [P3], 
    [c].[P4] AS [P4], 
    [c].[P5] AS [P5], 
    [c].[P6] AS [P6], 
    [c].[FixtureId] AS [FixtureId], 
    [c].[ExternTest] AS [ExternTest], 
    [c].[EnableTO] AS [EnableTO], 
    [c].[TOnr] AS [TOnr]
    FROM [dbo].[LaserData] AS [c]

0 个答案:

没有答案