2

在这个例子中:

using System;
using System.Collections.Generic;
using dbModel;
using System.Linq;
using System.Data.Entity.Infrastructure;


namespace WinApp
{
    public partial class Form1 : Form
    {
        private dbEntities dbc;
        public IQueryable<ARTIKLI> art;
        public IQueryable<ART_GRUPE> grp;

        public Form1()
        {
            InitializeComponent();
            dbc = new dbEntities();            
        }


        private void GetData()
        {
            art = from a in dbc.ARTIKLIs
                        select a;

            grp = from g in dbc.ART_GRUPE
                        select g;

            artikliBindingSource.DataSource = art.ToList();
            artGrupeBindingSource.DataSource = grp.ToList();
        }


        private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            dbc.SaveChanges();
        }


        private void loadData_Click(object sender, EventArgs e)
        {
            this.GetData();  
        }


        private void refresh_Click(object sender, EventArgs e)
        {

            dbc.Entry(grp).Reload();
            artGrupeBindingSource.ResetBindings(false);
        }

    }
}

一切正常。但是当我运行并单击刷新按钮时,我收到错误:

实体类型 DbQuery`1 不是当前上下文模型的一部分

我只是想grp使用DbContext. 我知道我可以转换DbContextObjectContext然后使用Refresh方法,但应该可以用DbContext.Entry(entity).Reload();

有人可以解释我在上面代码中的错误吗?

4

1 回答 1

7

如果dbEntities是 a DbContext,那么您正在谈论的是有效dbc.Entry<ART_GRUPE>().Reload();地丢弃所有更改并重新加载实体,而不是重新加载您的查询。然而,这并不存在。dbc.Entry<ART_GRUPE>(myEntity).Reload()您可以使用(“MSDN - DbEntityEntry<TEntity>.Reload Method” )放弃对单个实体所做的任何更改。

DbContext并不意味着长寿,您应该将它用于您的查询,然后摆脱它。如果要将其转换为对象上下文,可以尝试:

var ctx = ((IObjectContextAdapter)db).ObjectContext;
ctx.Refresh();

这可能不是您所需要的。它也不会从您的上下文中删除从数据库中删除的实体,并且它并不总是刷新关系。您最好摆脱上下文并再次加载它:

private void GetData()
{
    // you could wrap this in a using statement, though that isn't necessary
    using (var dbc = new dbEntities())
    {
        art = from a in dbc.ARTIKLIs
            select a;

        grp = from g in dbc.ART_GRUPE
            select g;

        artikliBindingSource.DataSource = art.ToList();
        artGrupeBindingSource.DataSource = grp.ToList();
    }
}
private void refresh_Click(object sender, EventArgs e)
{
    GetData();
    // not sure you need this next line now, but you should test
    artGrupeBindingSource.ResetBindings(false); 
}

这可能导致您的问题是您正在更改ARTIKLIs并尝试跟踪它们。为此,您可以使用以下内容来保存更改,并且不要ARTIKLIs每次都重新加载:

private void GetData(bool loadArtikli = true)
{
    // you could wrap this in a using statement, though that isn't necessary
    using (var dbc = new dbEntities())
    {
        if (loadArtikli)
        {
            art = from a in dbc.ARTIKLIs
                select a;
        }

        grp = from g in dbc.ART_GRUPE
            select g;

        artikliBindingSource.DataSource = art.ToList();
        artGrupeBindingSource.DataSource = grp.ToList();
    }
}
private void refresh_Click(object sender, EventArgs e)
{
    GetData(false);
}

public static void UpdateARTIKLI(ARTIKLI item)
{
  using (var dbc = new dbEntities())
  {
    if (item.Id > 0)
    { // update existing ones
      var dbitem = context.ARTIKLI 
        .Find(item.Id);

      context.Entry(dbItem)
        .CurrentValues
        .SetValues(item);
    }
    else
    { // deal with new ones
      context.ARTIKLI.Add(item);
    }

    context.SaveChanges();
  }
}
于 2013-06-01T12:52:09.873 回答