DbContext处理错误

时间:2013-07-29 16:09:25

标签: c# mysql visual-studio-2012 dbcontext

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

enter image description here

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

2 个答案:

答案 0 :(得分:0)

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

尝试这样的事情。

using(var context = new DataModel())
{
   //Prepare your Model
   var model = RecordsRepository.GetQueryableObject();
   //Call LoadRecords
   return LoadRecords(model);
}

答案 1 :(得分:0)

我找到了问题的解决方案。基本上我的一个对象是从表中接受错误的配置,无法初始化并抛出异常并最终处理上下文。 感谢大家的支持。