实体框架刷新背景? [英] Entity Framework Refresh context?

查看:213
本文介绍了实体框架刷新背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能刷新我的背景?我有实体的基础上,从我的数据库视图,当我在有导航性能视图中的一个表实体作出了更新,该实体是更新,但认为没有刷新符合新的更新......只是想再次得到数据库中的数据。
谢谢!

How could I refresh my context? I have entities based on views from my Database and when I made an update over one table Entity that has navigation properties to views, the entity is update but the view don't refresh accord the new updates...just want to get again from the Db the data. Thanks!

推荐答案

刷新实体上下文的最佳方法是处置上下文,并创建一个新的。

The best way to refresh entities in your context is to dispose your context and create a new one.

如果您的真正的需要刷新的一些实体和你正在使用code首先与方法类的DbContext,您可以用

If you really need to refresh some entity and you are using Code First approach with DbContext class, you can use

    public static void ReloadEntity<TEntity>(
        this DbContext context, 
        TEntity entity)
        where TEntity : class
    {
        context.Entry(entity).Reload();
    }

要重新加载集合导航属性,你可以使用

To reload collection navigation properties, you can use

    public static void ReloadNavigationProperty<TEntity, TElement>(
        this DbContext context, 
        TEntity entity, 
        Expression<Func<TEntity, ICollection<TElement>>> navigationProperty)
        where TEntity : class
        where TElement : class
    {
        context.Entry(entity).Collection<TElement>(navigationProperty).Query();
    }

参考:
<一href=\"https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry.reload(v=vs.113).aspx#M:System.Data.Entity.Infrastructure.DbEntityEntry.Reload\">https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry.reload(v=vs.113).aspx#M:System.Data.Entity.Infrastructure.DbEntityEntry.Reload

这篇关于实体框架刷新背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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