0

我已经尝试了互联网,但找不到一个好的答案。这是有问题的代码

在此处输入图像描述

    private void LoadRecords()
    {
        var data = from record in model.Records
                   where record.TimeStamp >= oldestRecordVisible
                   orderby record.TimeStamp descending
                   select record;
        dataGridView1.DataSource = data.ToList();
    } 

我正在为数据库使用 mySql 服务器。这是来自 context.cs 的代码

using System.Data.Entity;

namespace MilThicknessMonitor3.Models
{
public class DataModel : DbContext
{
    public DataModel()
        : base("name=DataConnection")
    {

    }
    public DbSet<Record> Records { get; set; }
    public DbSet<CaptureSource> CaptureSources { get; set; }
    public DbSet<PlcConfiguration> PlcConfigurations { get; set; }
    public DbSet<Configuration> Configurations { get; set; }

}

}

任何帮助将不胜感激

下面是数据模型

using MilThicknessMonitor3.Analytics;
using System;
using System.ComponentModel;

namespace MilThicknessMonitor3.Models
{
    public class Record
    {
        public int Id { get; set; }
        [DisplayName("Date/Time")]
        public DateTime TimeStamp { get; set; }
        [DisplayName("Average Measure (Mils)")]
        public double AvgMeasure { get; set; }
        [DisplayName("Average Measure (Millimetres)")]
        public double AvgMilli { get { return AvgMeasure / 39.37; } }
        [DisplayName("Minimum Measure (Mils)")]
        public double MinMeasure { get; set; }
        [DisplayName("Minimum Measure (Millimetres)")]
        public double MinMilli { get { return MinMeasure / 39.37; } }
        [DisplayName("Maximum Measure (Mils)")]
        public double MaxMeasure { get; set; }
        [DisplayName("Maximum Measure (Millimetres)")]
        public double MaxMilli { get { return MaxMeasure / 39.37; } }
        [DisplayName("Notes")]
        public string Notes { get; set; }

        #region Testing Fields, Properties, and Methods
        private DataLine offset;
        internal DataLine Offset
        {
            get { return offset; }
        }
        private DataLine baseline;
        internal DataLine Baseline
        {
            get { return baseline; }
        }
        private float[] results;

        public int[] OffsetValues
        {
            get
            {
                int[] values = new int[offset.Size];
                int index = 0;
                foreach (ImageColumn c in offset)
                {
                    values[index] = c.Ys.Count;

                    index++;
                }
                return values;
            }
        }

        public int[] BaselineValues
        {
            get
            {
                int[] values = new int[baseline.Size];
                int index = 0;
                foreach (ImageColumn c in baseline)
                {
                    values[index] = c.Ys.Count;

                    index++;
                }
                return values;
            }
        }

        public int[] ResultValues
        {
            get
            {
                int[] values = new int[results.Length];
                for (int i = 0; i < results.Length; i++)
                    values[i] = (int)Math.Round(results[i]);
                return values;
            }
        }

        internal void AddOffset(DataLine offset) { this.offset = offset; }
        internal void AddBaseLine(DataLine baseline) { this.baseline = baseline; }
        internal void AddResults(float[] results) { this.results = results; }
        #endregion
    }
}
4

2 回答 2

0

好吧,我找到了问题的解决方案。基本上,我的一个对象从表中获取了错误的配置,未能初始化并引发异常并最终处理上下文。感谢大家的支持。

于 2013-08-21T12:29:11.027 回答
0

您需要提供有关模型对象的更多信息。它是在哪里创建的?

尝试这样的事情。

using(var context = new DataModel())
{
   //Prepare your Model
   var model = RecordsRepository.GetQueryableObject();
   //Call LoadRecords
   return LoadRecords(model);
}
于 2013-07-29T16:34:30.057 回答