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



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

最佳答案

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

尝试这样的事情。

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

关于c# - dbContext处理错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17928866/

10-12 02:40