Linq查询中的'System.Data.Entity.Core.EntityCommandExecutionException'

坎蒂努

我正在开发我的第一个WPF浏览器应用程序。

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

由于加载需要花费几秒钟的时间,因此我尝试根据以下示例放置加载动画:

这里

我第一次导航到页面时不起作用。我的dataGrid保持为空。当我调试时,我在Get()函数的查询中遇到以下错误。

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

但是在我对动画进行更改之前,此查询曾经可以很好地工作。因此,问题可能不出于查询。

在此处输入图片说明

抛出:“连接必须有效且打开。” (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
}
布拉德利·乌夫纳

您收到的异常表示连接到数据库时出错。由于在应用程序的整个生命周期中保留单个Context引用的方式很难诊断出来。该连接在任何时候都可能失败。

对于这样的每个逻辑操作,请尝试将数据访问包装在新的Context中。在应用程序的整个生命周期中保持一个上下文不变是一种反模式,它可能导致各种错误,尤其是在尝试在后台执行操作时。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Linq查询中的“ System.Data.Entity.Core.EntityCommandExecutionException”

来自分类Dev

System.Data.Entity.Core.EntityCommandExecutionException不在查询中由用户代码处理

来自分类Dev

System.Data.Entity.Core.EntityCommandExecutionException不在查询中由用户代码处理

来自分类Dev

ASP.NET MVC System.Data.Entity.Core.EntityCommandExecutionException

来自分类Dev

c#Error as throw an exception of type'System.Data.Entity.Core.EntityCommandExecutionException'

来自分类Dev

类型'System.Data.Entity.Core.EntityCommandExecutionException'的第一次机会异常发生在EntityFramework.SqlServer.dll中

来自分类Dev

System.Data.EntityCommandExecutionException问题

来自分类Dev

ASP.NET Core中的System.Data.Entity.Spatial替换

来自分类Dev

从 Linq 查询(System.Data.Entity.Infrastructure.DbQuery)获取结果并转换为列表

来自分类Dev

无法使用类型为System.Data.Entity.Core.Objects.ObjectQuery的实例调用System.Data.Entity.DbSet

来自分类Dev

EntityCommandExecutionException:无效的列名'Entity_Id'

来自分类Dev

EntityFramework更改我的查询-EntityCommandExecutionException

来自分类Dev

无法将类型System.Data.Entity.Core.Objects.ObjectResult隐式转换为System.Data.Objects.ObjectResult

来自分类Dev

.NET Core 3.1,并且无法加载文件或程序集System.Data.Entity,版本= 4.0.0.0

来自分类Dev

不能使用类型为'System.Data.Entity.Core.Objects.ObjectQuery的实例进行调用

来自分类Dev

Mono中的System.Data.Entity.Design.PluralizationServices错误

来自分类Dev

Mono中的System.Data.Entity.Design.PluralizationServices错误

来自分类Dev

savechanges方法中的“ System.Data.Entity.Infrastructure.DbUpdateException”

来自分类Dev

Entity Framework Core Linq 查询返回数据库中不存在的 ID

来自分类Dev

无法将类型'System.Linq.IQueryable'隐式转换为'System.Data.Entity.DbSet'

来自分类Dev

Entity Framework Core中的动态查询执行

来自分类Dev

升级到EF 6(RTM)-获取System.Data.Entity.Core.Objects.ObjectContext不能用于返回类型System.Data.Objects

来自分类Dev

脚手架EntityFramework 6无法将类型为'System.Data.Entity.Core.Objects.ObjectContext'的对象转换为'System.Data.Objects.ObjectContext'

来自分类Dev

脚手架EntityFramework 6无法将类型为'System.Data.Entity.Core.Objects.ObjectContext'的对象转换为'System.Data.Objects.ObjectContext'

来自分类Dev

EF6不支持上下文类型'System.Data.Entity.Core.Objects.ObjectContext'

来自分类Dev

EF6不支持上下文类型'System.Data.Entity.Core.Objects.ObjectContext'

来自分类Dev

Entity Framework Core 2.1 System.Data.SqlClient.SqlException (0x80131904):类型标志不是定义的系统类型

来自分类Dev

System.Data.Spatial或System.Data.Entity.Spatial

来自分类Dev

无法将类型'System.Linq.IQueryable'隐式转换为'System.Data.Entity.Infrastructure.DbQuery'

Related 相关文章

  1. 1

    Linq查询中的“ System.Data.Entity.Core.EntityCommandExecutionException”

  2. 2

    System.Data.Entity.Core.EntityCommandExecutionException不在查询中由用户代码处理

  3. 3

    System.Data.Entity.Core.EntityCommandExecutionException不在查询中由用户代码处理

  4. 4

    ASP.NET MVC System.Data.Entity.Core.EntityCommandExecutionException

  5. 5

    c#Error as throw an exception of type'System.Data.Entity.Core.EntityCommandExecutionException'

  6. 6

    类型'System.Data.Entity.Core.EntityCommandExecutionException'的第一次机会异常发生在EntityFramework.SqlServer.dll中

  7. 7

    System.Data.EntityCommandExecutionException问题

  8. 8

    ASP.NET Core中的System.Data.Entity.Spatial替换

  9. 9

    从 Linq 查询(System.Data.Entity.Infrastructure.DbQuery)获取结果并转换为列表

  10. 10

    无法使用类型为System.Data.Entity.Core.Objects.ObjectQuery的实例调用System.Data.Entity.DbSet

  11. 11

    EntityCommandExecutionException:无效的列名'Entity_Id'

  12. 12

    EntityFramework更改我的查询-EntityCommandExecutionException

  13. 13

    无法将类型System.Data.Entity.Core.Objects.ObjectResult隐式转换为System.Data.Objects.ObjectResult

  14. 14

    .NET Core 3.1,并且无法加载文件或程序集System.Data.Entity,版本= 4.0.0.0

  15. 15

    不能使用类型为'System.Data.Entity.Core.Objects.ObjectQuery的实例进行调用

  16. 16

    Mono中的System.Data.Entity.Design.PluralizationServices错误

  17. 17

    Mono中的System.Data.Entity.Design.PluralizationServices错误

  18. 18

    savechanges方法中的“ System.Data.Entity.Infrastructure.DbUpdateException”

  19. 19

    Entity Framework Core Linq 查询返回数据库中不存在的 ID

  20. 20

    无法将类型'System.Linq.IQueryable'隐式转换为'System.Data.Entity.DbSet'

  21. 21

    Entity Framework Core中的动态查询执行

  22. 22

    升级到EF 6(RTM)-获取System.Data.Entity.Core.Objects.ObjectContext不能用于返回类型System.Data.Objects

  23. 23

    脚手架EntityFramework 6无法将类型为'System.Data.Entity.Core.Objects.ObjectContext'的对象转换为'System.Data.Objects.ObjectContext'

  24. 24

    脚手架EntityFramework 6无法将类型为'System.Data.Entity.Core.Objects.ObjectContext'的对象转换为'System.Data.Objects.ObjectContext'

  25. 25

    EF6不支持上下文类型'System.Data.Entity.Core.Objects.ObjectContext'

  26. 26

    EF6不支持上下文类型'System.Data.Entity.Core.Objects.ObjectContext'

  27. 27

    Entity Framework Core 2.1 System.Data.SqlClient.SqlException (0x80131904):类型标志不是定义的系统类型

  28. 28

    System.Data.Spatial或System.Data.Entity.Spatial

  29. 29

    无法将类型'System.Linq.IQueryable'隐式转换为'System.Data.Entity.Infrastructure.DbQuery'

热门标签

归档