Solved

C#: How to refresh Entity Framwork 6 entity list?

Posted on 2016-09-25
8
59 Views
Last Modified: 2016-09-29
I've got a class library that includes a static Account entity (it's called by an external program so almost everything has to be static). Account has a list of Alert entities among other things. I've now added a website where I can add, update and delete Alerts for each account to the database, but I'm struggling with figuring out how the class library should reload the Alerts on a timer.

Basically what I want to do is something like myaccount.Alerts.Reload(), but there is no Reload (or Refresh) function in EF 6.

Any suggestions?
TIA!
Dennis
0
Comment
Question by:Dennis_Gundersen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41815937
You can call the Refresh method of your object context as shown in https://msdn.microsoft.com/en-us/library/bb896255(v=vs.110).aspx:

YourObjectContext.Refresh(RefreshMode.StoreWins, YourEntity);

Open in new window

0
 

Author Comment

by:Dennis_Gundersen
ID: 41815942
Unfortunately, that function is gone in EF 6. db.Refresh(...), simply doesn't show up in my project. It must have been supplanted by something else, but I can't find it anywhere.

Re
Dennis
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41815948
what is db? Surely not an ObjectContext!
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:Dennis_Gundersen
ID: 41815952
Yes, "db" is my static variable for the DbContext. I believe the ObjectContext has been replaced some time ago.

Re
D
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41815956
A DbContext is not an ObjectContext! Convert it like this:
var context = new YourDbContext();
var adapter = (IObjectContextAdapter)context;
var objectContext = adapter.ObjectContext;

Open in new window

0
 

Author Comment

by:Dennis_Gundersen
ID: 41815976
OK, that's smart, but I still see one problem. I don't want to update the complete Account entity, since that would entail loading potentially millions on rows of Result and Order entities for that Account as well. I just want to reload myaccount.Alerts (which would probably be 5-6 rows only).
I'm thinking it may be possible with something like this:

            using (var context = new HGContext())
            {
                var adapter = (IObjectContextAdapter)context;
                var objectContext = adapter.ObjectContext;
                objectContext.Refresh(System.Data.Entity.Core.Objects.RefreshMode.StoreWins, acc.Alerts);
            }

Open in new window

I'm then assuming that this would take my static Account entity and only refresh it's Alerts collection. Am I understanding this correctly?

Re
D
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41815990
I have situations where I refresh only one entity (table). What you wrote (acc.Alerts) should work. If you want to be sure of what is getting refreshed, open the SQL Profiler to see the query.
0
 

Author Comment

by:Dennis_Gundersen
ID: 41815995
Alright then, this sounds very promising. It's going to take me a while to actually test this out completely, but I'm hopeful and will just add another question of something should fail with the finer details.
Thank you very, very much!
Re
Dennis
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question