实体框架刷新上下文? [英] Entity Framework Refresh context?

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

问题描述

如何刷新我的上下文?我有基于我数据库中的视图的实体,当我对一个具有视图导航属性的表 Entity 进行更新时,该实体正在更新,但视图不会根据新的更新进行刷新......只是想再次从Db 数据.谢谢!

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.

如果您真的需要刷新某些实体并且您正在使用带有 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();
    }

参考: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天全站免登陆