Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Refresh context? [closed]

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!

like image 758
user2528557 Avatar asked Nov 28 '13 16:11

user2528557


People also ask

How do I update a single table in EDMX?

There is way of doing it automatically. right click edmx file > update model from data base > Refresh tab > Tables > select the table(you want to update) and press finish that's it.

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.


2 Answers

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

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

Reference: https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry.reload(v=vs.113).aspx#M:System.Data.Entity.Infrastructure.DbEntityEntry.Reload

like image 165
RX_DID_RX Avatar answered Sep 29 '22 10:09

RX_DID_RX


yourContext.Entry(yourEntity).Reload(); 
like image 45
kravits88 Avatar answered Sep 29 '22 10:09

kravits88