我不明白EF5 dbContext.Entry(实体).Reload()方法是如何工作的? [英] I do not understand how EF5 dbContext.Entry(entity).Reload() method is supposed to work?

查看:2754
本文介绍了我不明白EF5 dbContext.Entry(实体).Reload()方法是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个例子:

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

    }
}



一切建立好。但是,当我运行,并单击刷新按钮,我得到错误:

everything builds OK. But when I run and click Refresh button I get error :

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

The entity type DbQuery`1 is not part of the model for the current context

我只是想刷新从存储数据为使用GRP 实体实例的DbContext 。我知道我可以转换的DbContext 的ObjectContext ,然后用刷新方法,但
就应该可以做同样的 DbContext.Entry(实体).Reload();

I am just trying to refresh data from store for grp entity instance using DbContext. I know I can convert DbContext into ObjectContext and then use Refresh method, but it should be possible to do the same with DbContext.Entry(entity).Reload();

有人能解释上面的代码我的错误?

Can someone explain my mistakes in the code above?

推荐答案

如果 dbEntities 的DbContext < )(重载(); / code>,那么你在谈论这样做,有效地 dbc.Entry< ART_GRUPE过夜。,放弃所有更改并重新加载实体不重装您的查询。但是,这不存在。您可以放弃使用 dbc.Entry<单个实体所做的任何更改; ART_GRUPE>(myEntity所).Reload()MSDN - DbEntityEntry< TEntity> .Reload方法)。

If dbEntities is a DbContext, then you are talking about doing, effectively dbc.Entry<ART_GRUPE>().Reload();, to discard all changes and reload the entities, not reloading your query. However, this does not exist. You can discard any changes made to a single entity using the dbc.Entry<ART_GRUPE>(myEntity).Reload() ("MSDN - DbEntityEntry<TEntity>.Reload Method").

的DbContext 并不意味着要长期生活,你打算用它来查询,然后摆脱它。如果你想将它转换为对象上下文,你可以尝试:

DbContext is not meant to be long lived, you are meant to use it for your query, then get rid of it. If you want to cast it to an object context, you could try:

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

这可能不是你所需要的。它也不会删除从上下文分贝删除的实体,它并不总是刷新关系。你可能会更好摆脱环境并重新装入:

This may not be what you need. It also doesn't remove entities deleted from the db from your context, and it doesn't always refresh relationships. You may be better off getting rid of the context and loading it again:

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

这可能会导致您的问题是,你对<$ C更改$ C> ARTIKLIs ,并试图追踪它们。对于您可以使用类似下面的保存更改,不重新加载 ARTIKLIs 每次:

The problem this may cause you is that you are making changes to ARTIKLIs and trying to track them. For that you could use something like the following to save changes, and do not reload your ARTIKLIs each time:

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

这篇关于我不明白EF5 dbContext.Entry(实体).Reload()方法是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆