2015-11-20 83 views
3

我正在开发我的第一个WPF浏览器应用程序。Linq Query中的'System.Data.Entity.Core.EntityCommandExecutionException'

我在dataGrid中加载发票,然后使用textBox或comboBox进行过滤。

,因为它需要几秒钟的加载,我'试图把加载动画按照下面的例子:

here

它不工作,我第一次访问页面。我的dataGrid保持空白。当我调试时,我有以下错误发生在Get()函数的查询中。

“System.Data.Entity.Core.EntityCommandExecutionException” mscorlib.dll中发生,但在用户代码中

但此查询使用之前,我做了动画的变化很好地工作没有处理。所以也许问题不是来自查询。

enter image description here

异常:抛出该异常: “连接必须是有效的和开放的。” (System.InvalidOperationException) System.InvalidOperationException被抛出:“连接必须有效且打开。” 时间:2015年11月20日下午12时36分31秒 主题:工作线程[13324]

public class ConsultInvoiceViewModel : ViewModelBase 
{ 

    public Context ctx = new Context(); 

    private ICollectionView _dataGridCollection; 
    private string _filterString; 
    private ObservableCollection<Invoice> invoiceCollection; 


    public ConsultInvoiceViewModel() 
    { 
     if (!WPFHelper.IsInDesignMode) 
     { 
      var tsk = Task.Factory.StartNew(InitialStart); 
      tsk.ContinueWith(t => { MessageBox.Show(t.Exception.InnerException.Message); }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext()); 
     } 
    } 

    private void InitialStart() 
    { 
     try 
     { 
      State = StateEnum.Busy; 
      DataGridCollection = CollectionViewSource.GetDefaultView(Get()); 
      DataGridCollection.Filter = new Predicate<object>(Filter); 
      GetShop(); //load one comboBox 
      GetSupplier(); //load one comboBox 
     } 
     finally 
     { 
      State = StateEnum.Idle; 
     } 

    } 

    private ObservableCollection<Invoice> Get() 
    { 
     DateTime date2 = DateTime.Now.AddMonths(-2); 

     var query = ctx.Invoices 
        .GroupBy(x => new { x.suppInvNumber, x.shop1, x.date, x.foodSupplier }) 
        .ToList() 
        .Select(i => new Invoice 
        { 
         suppInvNumber = i.Key.suppInvNumber, 
         shop1 = i.Key.shop1, 
         date = i.Key.date, 
         foodSupplier = i.Key.foodSupplier, 
         totalPrice = i.Sum(t => t.totalPrice), 
        }) 
        .Where(d => d.date >= date2) 
        .OrderByDescending(d => d.date) 
        .AsQueryable(); 

     invoiceCollection = new ObservableCollection<Invoice>(query); 

     return invoiceCollection; 
    } 

    public ICollectionView DataGridCollection 
    { 
     get 
     { 
      return _dataGridCollection; 
     } 
     set 
     { 
      _dataGridCollection = value; 
      OnPropertyChanged("DataGridCollection"); } 
    } 

    public string FilterString 
    { 
     get 
     { 
      return _filterString; 
     } 
     set 
     { 
      _filterString = value; 
      OnPropertyChanged("FilterString"); 
      FilterCollection(); 
     } 
    } 


    public static readonly PropertyChangedEventArgs StateArgs = ViewModelBase.CreateArgs<ConsultInvoiceViewModel>(c => c.State); 
    private StateEnum _State; 

    public StateEnum State 
    { 
     get 
     { 
      return _State; 
     } 
     set 
     { 
      var oldValue = State; 
      _State = value; 
      if (oldValue != value) 
      { 
       OnStateChanged(oldValue, value); 
       OnPropertyChanged(StateArgs); 
      } 
     } 
    } 

    protected virtual void OnStateChanged(StateEnum oldValue, StateEnum newValue) 
    { 
    } 



    private void FilterCollection() 
    { 
     if (_dataGridCollection != null) 
     { 
      _dataGridCollection.Refresh(); 
     } 
    } 

    private bool Filter(object obj) 
    { 
     var data = obj as Invoice; 

     if (data != null) 
     { 
      if (!string.IsNullOrEmpty(_filterString)) 
      { 
       return data.suppInvNumber.Contains(_filterString); 

      } 
      return true; 
     } 
     return false; 
    } 

    private void SearchFilter() 
    { 
     IOrderedEnumerable<Invoice> invs; 
     invs = ctx.Invoices 
        .Where(s => s.shop == Shop && s.supplier == Supplier && s.date >= From && s.date <= To) 
        .GroupBy(x => new {x.suppInvNumber, x.shop1, x.date, x.foodSupplier }) 
        .ToList() 
        .Select(i => new Invoice 
        { 
         suppInvNumber = i.Key.suppInvNumber, 
         shop1 = i.Key.shop1, 
         date = i.Key.date, 
         foodSupplier = i.Key.foodSupplier, 
         totalPrice = i.Sum(t => t.totalPrice), 
        }) 
        .OrderByDescending(d => d.date); 
     } 

     invoiceCollection.Clear(); 
     if (invs != null) 
      foreach (var inv in invs) 
      { 
       invoiceCollection.Add(inv); 
      }  
     FilterCollection(); 
    } 

    #region combobox 
    private void GetShop() 
    { 
     ctx.shops.ToList().ForEach(shop => ctx.shops.Local.Add(shop)); 
     SShop = ctx.shops.Local; 
    } 

    private void GetSupplier() 
    { 
     ctx.foodSuppliers.ToList().ForEach(supplier => ctx.foodSuppliers.Local.Add(supplier)); 
     FoodSupplier = ctx.foodSuppliers.Local; 
    } 


    private IList<foodSupplier> supplier; 

    public IList<foodSupplier> FoodSupplier 
    { 
     get 
     { 
      if (supplier == null) 
      GetSupplier(); 
      return supplier; 
     } 
     set 
     { 
      supplier = value; 
      OnPropertyChanged("FoodSupplier"); 
     } 
    } 

    private IList<shop> shop; 

    public IList<shop> SShop 
    { 
     get 
     { 
      return shop; 
     } 
     set 
     { 
      shop = value; 
      OnPropertyChanged("SShop"); 
     } 
    } 


    private int _shop; 

    public int Shop 
    { 
     get 
     { 
      return _shop; 
     } 
     set 
     { 
      _shop = value; 
      OnPropertyChanged("Shop"); 
      SearchFilter(); 
     } 
    } 

    private int _supplier; 

    public int Supplier 
    { 
     get 
     { 
      return _supplier; 
     } 
     set 
     { 
      _supplier = value; 
      OnPropertyChanged("Supplier"); 
      SearchFilter(); 
     } 
    } 

    #endregion 

    #region "Command" 

    private ICommand searchCommand; 

    public ICommand SearchCommand 
    { 
     get 
     { 
      return searchCommand ?? (searchCommand = new RelayCommand(p => this.Search(), p => this.CanSearch())); 
     } 
    } 

    private bool CanSearch() 
    { 
     return true; 
    } 

    #endregion 
} 
+1

我的猜测是对'.AddDate'的调用有问题。 Entityframework不知道如何将其转换为sql。尝试将查询之外的日期预先计算为简单的日期变量并使用它。 –

+0

检查这个http://stackoverflow.com/questions/31118257/an-exception-of-type-system-data-entity-core-entitycommandexecutionexception-o – 2015-11-20 03:56:06

+0

和这个http://stackoverflow.com/questions/25582291/a -first-chance-exception-of-type-system-data-entity-core-entitycommandexecution – 2015-11-20 03:56:22

回答

2

你所得到的异常指示连接到数据库中的错误。由于您为应用程序的整个生命周期保留单个上下文引用,因此很难诊断此问题。任何时候这种联系都可能失败。

尝试将每个逻辑操作的数据访问封装在新的上下文中。在应用程序的生命周期中保持一个上下文是一种反模式,可能会导致各种错误,尤其是在尝试在后台执行某些操作时。

private ObservableCollection<Invoice> Get() 
    { 
     using (var ctx = new Context()) 
     { 
     DateTime date2 = DateTime.Now.AddMonths(-2); 

     var query = ctx.Invoices 
        .GroupBy(x => new { x.suppInvNumber, x.shop1, x.date, x.foodSupplier }) 
        .ToList() 
        .Select(i => new Invoice 
        { 
         suppInvNumber = i.Key.suppInvNumber, 
         shop1 = i.Key.shop1, 
         date = i.Key.date, 
         foodSupplier = i.Key.foodSupplier, 
         totalPrice = i.Sum(t => t.totalPrice), 
        }) 
        .Where(d => d.date >= date2) 
        .OrderByDescending(d => d.date) 
        .AsQueryable(); 

     invoiceCollection = new ObservableCollection<Invoice>(query); 
     } 
     return invoiceCollection; 
    } 
+0

有效!谢谢。我不知道! – Cantinou

相关问题